Re: Accessing C++ from C

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 01/08/04


Date: Thu, 08 Jan 2004 17:43:32 +0100

Evan Carew wrote:

> 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.

Actually, there are C++ wrappers for the GNOME libs (e.g. gtkmm) and
some gnome programs are actually written in C++. There are also quite
some Un*x libs that are written in C++ and used from C programs, or
even plugin systems where the plugins and the main program are not
written in the same language, so glueing C code and C++ code together
isn't actually that uncommon.

> 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++.

Some developers want to use C rather than 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.

That's what I told you. Just look yourself at what happens. When trying
to link with gcc and with g++, add the -v command line option, and the
compiler will tell you what it does and what command line it passes to
the linker.



Relevant Pages

  • Re: back online again...
    ... I'd then have the compiler automatically fill in "dummy" ... dynamically compiled code uses this convention as if it were ... a custom prolog and epilog or wrapper function for calling SysV ... internal C functions and the SysV calling convention for external C ...
    (alt.lang.asm)
  • I need help with HALD...
    ... and installed from scratch with reference to the FreeBSD Handbook. ... Compiler options used: ... Clicking on any desktop icon in Gnome yields no response. ... Disabling HALD in /etc/rc.conf makes x.org/Gnome work again. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: (n)curses problem compiling tty-only nethack on OSX 10.4
    ... > Yes, and it also says the tiled OS X version doesn't use the sys/mac tree, ... with gcc for the mac. ... replies in message# 304595 why? ... readme is talking about the Metrowerks compiler alone. ...
    (rec.games.roguelike.nethack)
  • repost: problem compiling Managed Sink Wrapper for SMTP
    ... (I'm sorry to keep posting this question, but I only just figured out that I ... Unfortunaltely this resulted in compiler errors due to some ... datatype mismatches in the calls from the wrapper class to the interop DLL. ...
    (microsoft.public.exchange2000.development)
  • problem compiling Managed Sink Wrapper for SMTP
    ... (I've already posted wthis question to a couple of other newsgroups but have ... Our team needs to recieve SMTP mail from within a C# app, ... Unfortunaltely this resulted in compiler errors due to some ... datatype mismatches in the calls from the wrapper class to the interop DLL. ...
    (microsoft.public.exchange2000.development)