automake
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Linking in C++ mode


From: Ralf Corsepius
Subject: Re: Linking in C++ mode
Date: 11 Nov 2002 05:36:57 +0100

Am Son, 2002-11-10 um 16.31 schrieb John Levon:
> On Sun, Nov 10, 2002 at 01:01:08PM +0100, Ralf Corsepius wrote:
> 
> > > You could always just add -lstdc++ to the oprofiled_LDADD variable.
> > This would be a fault, IMO.
> 
> I do not think getting gcc to link -lstdc++ is a good idea either, but
> it doesn't have much more hack value than the solution you propose below
> IMHO.
> 
> > The problem is trying to link a c-program against a c++-library. This
> > doesn't work in general, esp. not with g++, unless such a c++ library is
> > specially designed for such purposes.
> 
> Hmm, care to expand ? The library has an extern "C" interface used by the
> C program, and an internal implementation coded in C++.
Fine, this is one prerequisite, but there is more ...

E.g. in general, C++ requires special low level startup
initialization(s). One occasion where you typically encounter this issue
is using static constructors. 

To avoid such situations, you'd have to avoid static constructors and
carefully analyze if other libs your lib depends upon would pull them
in. If depending upon libstdc++, there normally isn't a general way to
avoid them at all (You don't have control on what libstdc++ interally
does.).

> > Unresolved references to libstdc++
> > indicate that your library has not been prepared for this.
> 
> Are you seriously saying I have to statically link the library ?
No, this would be a mis-interpretation.

> > A proper solution would be to use g++ to link the application. To
> > achieve this with automake, the easiest way is to convert the
> > main-application file to c++. In your case, renaming oprofiled.c to
> > oprofiled.cc would be sufficient.
> 
> This seems IMHO to be a terrible hack. I've found automake to be quite
> flexible so far; am I hitting one of its limitations ?
Well, actually you are facing 2 problems:

1. Finding a way to compile your program (independently of automake). 

You are using a library written in c++, therefore a general solution is
using "g++" to _link_ your program, even if your "main" is written in C.

[Using gcc -lstdc++ is known to have worked for such cases with gcc <
3.0, but is known not to work for gcc >= 3.0.
Rule of thumb: If c++ is involved somewhere, invoke "g++" instead of
"gcc" for linking.
]

2. Finding a way to convince automake to using "g++" to link a you
program. 

Automake guesses upon the linker by using file name extensions. Your
"main" is called "*.c", so it tries to use "gcc" instead of "g++".

One way to work around this problem is renaming your "main" a c++ file
name. Another way is to dive into automake internals and to override the
linker (CXXLINK and CXXLD vs CCLINK and CCLD).

Ralf





reply via email to

[Prev in Thread] Current Thread [Next in Thread]