Re: Accessing C++ from C

From: Evan Carew (tempcarew_at_pobox.com)
Date: 01/08/04


Date: Thu, 08 Jan 2004 09:07:48 -0500

Rolf, (and all others who replied with advice of varying degrees of
usefullness)

Thanks for your replies. While I generally don't like people who answer
their own questions, I have a feeling that this question is important
enough to the C++ and C community that it needs to be answered. I say
this because if C developers (or C++ developers) don't have a way to
gradually migrate their legacy code (C) to C++ then we will continue in
our current state of affairs we are in today where people like the
developers of GNOME continue to develop in C. With this technique,
someone could refactor their libraries in C++ and provide a C wrapper
for those still using legacy techniques, while newer developers could go
on to use C++.

Yesterday, I sent a message to the author of C/C++ Users Journal article
I mentioned in my query & he replied with the answer last night. It
turns out to be rather easy. The deal is that while the C compiler is
more than happy to compile the legacy code, and the C++ compiler
likewise happy to compile the wrapper code, the C linker simply wont
link the program. The strange fix is to compile the legacy code with
the C compiler (without linking), then link with g++. Voila, you have a
C front end to C++ library code.

So now, whenever I talk to some C jock who swears there is no way for C
to converse with C++, I'll simply point them to my interface code.

P.S.
If you are interested in a working swample project, I can send you a
gziped tar file which you can compile yourself.

Evan

Rolf Magnus wrote:
> Evan Carew wrote:
>
>
[snip]
>
>
> This is actually off-topic here, and should be asked in gnu.gcc.help or
> gnu.g++.help, but anyway, you need to use g++ for linking if your
> program contains c++ code.
>



Relevant Pages

  • Re: additional requirements for a Ruby env
    ... decent compiler toolchain to compile itself) then developers would be freed to ... compile them up themselves. ... cross platform image magic installation when the build process of ruby itself ... suffering increases your inner strength. ...
    (comp.lang.ruby)
  • Re: Programmers unpaid overtime.
    ... > I need to avoid a practical problem which other developers ignore. ... practical example (which will compile) and your alternative that is ... you apply them after NOT using precise terminology and getting ... > then waste thousands of man-hours when applied to real data sets. ...
    (comp.programming)
  • Dear ms VS.NET 2005 development team
    ... EVERY team I work with and know of (over 400 developers) has decided to ... cannot be productive waiting 3 minutes to compile a simple winform. ... will always be dog slow. ... if you know that certain parts of the IDE are causing major perormance ...
    (microsoft.public.vsnet.general)
  • Re: Difficulty Reading in Constants
    ... The legacy code has parameters which are set in the .f file itself i.e. you modify the text file, ... and since you're reading those values in, they're no longer parameters and, thus, they can no longer be used to dimension the arrays. ... It's not a big problem, but my approach would be to compile the code with g95 after modifying the code to make the arrays in question declared with the attribute ALLOCATABLE, e.g. ... Invalid declaration of or reference to symbol `mdim' at [initially ...
    (comp.lang.fortran)
  • Re: cxx performance
    ... > to compile on the PC. ... timer support and other stuff requiring the ... > If you want VMS to continue to live, the developers tools have to ... compilation/linking speeds were an order of magnitude faster I could ...
    (comp.os.vms)