Linking dependencies into static library / Partial linking

Dave Korn dave.korn@artimi.com
Sun Jan 13 07:52:00 GMT 2008


On 11 January 2008 17:04, Philip K.F. Hölzenspies wrote:

> When running "make allcpp" you'll notice that the functions in cwrapper.cc
> and foo.h aren't found by the linker:
> 
> g++ -L. bar.c -lfoo -o bar
> /tmp/cc6lYrCK.o: In function `main':
> bar.c:(.text+0x12): undefined reference to `newFoo()'
> bar.c:(.text+0x1a): undefined reference to `newFoo()'
> bar.c:(.text+0x28): undefined reference to `doFoo(CFoo*)'
> bar.c:(.text+0x33): undefined reference to `doFoo(CFoo*)'
> bar.c:(.text+0x3e): undefined reference to `freeFoo(CFoo*)'
> bar.c:(.text+0x49): undefined reference to `doFoo(CFoo*)'
> bar.c:(.text+0x54): undefined reference to `freeFoo(CFoo*)'
> 
> However, "nm libfoo.a" disagrees:
> 
> holzensp@ewi1043:~/tmp/ldtest> nm libfoo.a
> 
> foo.o:
> 00000180 t _GLOBAL__I__ZN4CFooC2Ev
>          U _Unwind_Resume
> 00000140 t _Z41__static_initialization_and_destruction_0ii
> 0000001c T _ZN4CFoo5doFooEv
> 0000000e T _ZN4CFooC1Ev
> 00000000 T _ZN4CFooC2Ev
> 000001b0 T _ZN4CFooD1Ev
> 000001f2 T _ZN4CFooD2Ev
>          U _ZNKSs4sizeEv
>          U _ZNKSsixEj
>          U _ZNSolsEi
>          U _ZNSt8ios_base4InitC1Ev
>          U _ZNSt8ios_base4InitD1Ev
> 00000034 t _ZSt17__verify_groupingPKcjRKSs
> 00000000 W _ZSt3minIjERKT_S2_S2_
>          U _ZSt4cout
> 00000000 b _ZSt8__ioinit
>          U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
>          U _ZdlPv
>          U _Znwj
>          U __cxa_atexit
>          U __dso_handle
>          U __gxx_personality_v0
> 0000019c t __tcf_0
> 0000025e T doFoo
> 00000234 T freeFoo
> 00000272 T newFoo
> 
> 
> Notice that the names are, indeed, unmangled, so the difference between the
> C and C++ interpretations of the CFoo type should not be an issue.

  I disagree.  Add "--save-temps" to the commandline:

g++ --save-temps -L. bar.c -lfoo -o bar

then look at the symbols required by bar.o:

/artimi/software/firmware/build/ldtest $ nm bar.o
00000000 b .bss
00000000 d .data
00000000 t .text
         U __Z5doFooP4CFoo
         U __Z6newFoov
         U __Z7freeFooP4CFoo
         U ___main
         U __alloca
00000000 T _main

  It's looking for the mangled versions; the library, as you point out, has
only unmangled versions.  If you change the start of bar.c from:

   ...snip...
#include "foo.h"

int main () {
   ...snip...

to this:

   ...snip...
extern "C" {
#include "foo.h"
}

int main () {
   ...snip...


it will link ok.  (Don't forget to manually pass 'this' as the first arg when
calling C++ non-static class member functions from C, of course.)



    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....



More information about the Binutils mailing list