help-gplusplus
[Top][All Lists]
Advanced

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

g++ linker error linking dependent static libraries to create executabl


From: sriram . sundararajan
Subject: g++ linker error linking dependent static libraries to create executable
Date: Thu, 24 Jan 2008 18:23:22 -0800 (PST)
User-agent: G2/1.0

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.

blah/
  /libA
      libA.h      libA.cpp     -----> libA.a <= A::printA(string arga)
  /libB
      libB.h      libB.cpp    ------> libB.a <= B::printB(string argb)
{ A().(argb) }
  /src
       hello.cpp

LibA contains class A, with method A::printA(string arga).
LibB contains class B with method B::printB(string argb) which calls
A().printA(argb).
hello.cpp invokes B().printB("Hello, World"). When I compiled libB, I
did not link with libA.a as I wanted to keep the libraries separate.
My "real world" libraries are way way larger.

So I created static libraries libA.a and libB.a. When I tried to
create hello.exe using,

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)'
collect2: ld returned 1 exit status
make: *** [debug/main.exe] Error

I am sure
1. I used my -L and -l options right
2. I used nm to verify the symbols are actually in both the libraries.
(A in libA and B in libB. There are 'U'ndefined references to A in
libB though).
3. The methods are public
4. I tried ordering the libraries differently in the -l option. Here
there are only 2 possibilities I guess.

I am using MinGW. Any suggestions? Why does this occur? My google-fu
has failed me the past couple of days. I would like to keep the
libraries separate. combining libs A and B does work. I'm puzzled as
to why this approach does not work. The symbol definitions are
definitely present in both the libraries.

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.

Thanks
Sriram


reply via email to

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