help-gplusplus
[Top][All Lists]
Advanced

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

Re: g++ linker error linking dependent static libraries to create execut


From: Bernd Strieder
Subject: Re: g++ linker error linking dependent static libraries to create executable
Date: Fri, 25 Jan 2008 09:45:20 +0100
User-agent: KNode/0.10.4

Hello,

sriram.sundararajan@gmail.com wrote:

> I'm battling a linker issue. Here is the scenario. I have two static
> libraries libA.a and libB.b and am trying to create an executable
> hello.exe from hello.cpp, libA.a and libB.a.

> g++ -W -Wall -pipe -g -o main.exe main.o  -Lblah/src -Lblah/lib1 -
> Lblah/lib2 blah/lib1/libA.a blah/lib2/libB.a
> 
> I got this....
> 
> blah/lib2/libB.a(libB.o): In function `ZN1B1aESs':
> blah/lib2/libB.cpp:9: undefined reference to `A::printB(std::string)'
> blah/lib2/libB.a(libB.o): In function `ZN1B1bEi':
> blah/lib2/libB.cpp:15: undefined reference to `A::printA(int)'

libB depends on libA, so libA should go after libB in the command line,
or you have to use the --start-group --end-group options. The GNU
linker (and possibly most other linkers) treats all linked files in the
command-line in the order they are given, not returning to an earlier
one automatically. In your case you might get away most easily by
linking libA once before libB and again after it.

It is generally best to avoid circular references, since it is prone to
errors and whatever you do, it adds overhead to the linking process.

> 
> I am guessing that since hello calls B::b, libB gets linked first and
> the process of linking, the linker(?) tries to lookup all symbols in
> B, including A. Since A has not been linked in, we are getting this
> error. I tried defining A first in hello.cpp and invoked A().printA
> before invoking B. This actually took care of the linker errors. I
> Can't do that in real life! I would also prefer to keep the libraries
> static.

Bernd Strieder



reply via email to

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