Wrapping C++ in C.

Phil Edwards pedwards@jaj.com
Thu Feb 3 17:22:00 GMT 2000

 Kevin Atkinson <kevinatk@home.com> writes:
> On Thu, 3 Feb 2000, Joe Buck wrote:
> > > 1) According to the C++ Faq lite when mixing C and C++ code there are
> > > several caveats:
> > > * Your C++ compiler should direct the linking process (e.g., so it can 
> > >   get its special libraries) 
> > 
> > You can use gcc rather than g++ to link, provided that you add
> > 	-lstdc++ -lm
> > to the end of your link line.
> What is the C++ code is in a shared library?
On the assumption that you meant to type "if" there, then you would treat
the C++ library like any other library, e.g., object files and libraries
that use symbol <FOO> should precede those that define symbol <FOO>,
for maximum portability.

As far as g++'s code, the "stdc++" that Joe mentioned above /is/
the library.

> > > How much of this applies on a Posix system?
> > 
> > If you want to let people use other C++ compilers than g++, all of it.
> Um, maybe I am just being stupid but don't most unixes have a standard C
> calling convention?  If not how is it that gcc can use standard system
> libraries compiled by a different C compiler?

In my experience, it's not so much a "standard calling convention"
as it is "C symbols generally aren't name-mangled."

Also, the program that actually uses those libraries is the link editor,
not the compiler, and gcc is often (and by default) built to use the
system's native linker anyhow; it would know how to read those libraries.
(Mixing C++ libraries, on the other hand, is often an exercise in
masochism; cf. for example the recent thread on these lists regarding
symbol screwups with IRIX's standard C++ library.)

> Also how do static variables in those libraries get initialized?
> > > 2) Is it possible to get a function pointer out of a C++ member
> > > function?
> > 
> > These are general C++ language questions, not for this list.  A pointer
> > to member function and a pointer to function are very different beasts.
> Yes I know that.  That was NOT my question.  I am looking for a gcc
> specific answer.  As I understand it member functions are almost like
> normal functions except that there is a hidden this parameter and for
> virtual member functions there is also a lookup on the virtual table.

Nothing that you've said there is gcc-specific.  The same caveats apply
no matter what compiler is being used, and the syntax will be the same
no matter what compiler is being used; Joe's answer is still true.
I recommend searching comp.lang.c++[.moderated] and comp.std.c++
in DejaNews.


More information about the Gcc mailing list