From owner-freebsd-questions@FreeBSD.ORG Fri Feb 24 19:13:02 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B1D33106566C for ; Fri, 24 Feb 2012 19:13:02 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (mx-out.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id 75D038FC0C for ; Fri, 24 Feb 2012 19:13:01 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id q1OJDWLp052080 for freebsd-questions@freebsd.org; Fri, 24 Feb 2012 13:13:32 -0600 (CST) Date: Fri, 24 Feb 2012 13:13:32 -0600 (CST) From: Robert Bonomi Message-Id: <201202241913.q1OJDWLp052080@mail.r-bonomi.com> To: freebsd-questions@freebsd.org In-Reply-To: <201202241207.q1OC75HG062038@fire.js.berklix.net> Subject: Re: Converting C++ to C X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Feb 2012 19:13:02 -0000 "Julian H. Stacey" wrote; > perryh@pluto.rain.com wrote: > > Reason: I want to make what I think would be a fairly minor > > change to a small (1100-line) C++ program, but I don't know C++ > > -- only C -- and I don't understand the program well enough > > to mess with it. I suspect I would be able to figure out an > > equivalent C program. > > > > In case it matters, I'm using FreeBSD 8.1. > > One of the lists recently (maybe 2/3 weeks ago) carried a thread > listing many C compilers past & present. It started by discussing > Clang V. GCC I can't remember which list, I don't think it was > questions@ maybe hackers@ or current@. There _was_ a recent discussion on 'questions' -- I'm the 'guilty party' responsible for naming a lot of the 'historical' ones. That aside, for the OP: C code generated from C++ will _not_ be very readable. Basically, -everything- in C++ would get turned into a function invocation in the generated C. With the _name_ of each such function having an encoded representation of the type of each argument to that function (see 'function name mangling). And the "simple" elementary data types tend to end up as something like: "**struct foo {bar value; (*(**struct foo)baz())[];}". Some of the mayhem: _everything_ is 'double indirect' pointers, to support run-time automatic garbage collection; 'methods' of acting on data elements are pointers to functions, embedded in the data-element structure, even basic 'four function calculator' arithmetic ops (they can be 'overlaid' to do differnt things on different data types -- the '+' operator may mean 'concatenation' when applied to two strings, or '+=' maay mean 'append item to list, in the contest of 'list += item', even though both would *still* mean 'addition' when used with numeric items.) One would be far better off spending some time to learn the basics of C++ syntax -- to be able to 'read' the existing code and understand what it's doing. After that, if what you want to modiy -is- truely a 'minor' change, adding some 'C-tyee' code to implement it is probably not that bad.