1

I have developed a shared library B.so, which depends on A.so. When I write a program test.exe using B.so, but there is a compile error, it said that some symbols(the symbols are in A.so) not found. My build line:

gcc test.c -o test.exe -fPIC -I./ -L./ -lB

Do we have a method that, how to build test.exe successfully,but not link A.so.

4
  • Have you tried linking with A as well? Commented Apr 24, 2014 at 14:07
  • yes, if I add the A.so, it will be OK. I want to build a SDK b.so, however I don't want building test.exe needs A.so
    – iceKing
    Commented Apr 24, 2014 at 15:05
  • So you want the symbols (variables/functions/etc.) that are in your A library to just magically appear in anything linked with library B? That's not likely with most compilers. You could potentially include A in B in its entirety to get around that, though...
    – twalberg
    Commented Apr 24, 2014 at 15:17
  • @twalberg Some time ago GNU ld had a different behavior in this kind of situation, it did link to A.so automatically. But it does not do it anymore.
    – Lee Duhem
    Commented Apr 24, 2014 at 15:32

1 Answer 1

1

how to build test.exe successfully,but not link A.so.

There are at least two method:

  1. export a proper LD_LIBRARY_PATH

    export LD_LIBRARY_PATH=/path/to/A
    gcc ... -lB
    
  2. using ld option -rpath (discovered by the asker @iceKing himself)

    gcc -Wl,-rpath=/path/to/A ...
    

In both case, ld will automatically search for libraries depended by these libraries list explicitly.

6
  • Thanks for your reply. However when build B.so, I use the link "-lrt -lcurl", but when build test.exe, they are not needed? why?
    – iceKing
    Commented Apr 24, 2014 at 14:54
  • @iceKing Did you really use function from libcurl.so in your B.so?
    – Lee Duhem
    Commented Apr 24, 2014 at 15:04
  • Yes, maybe it choose the static library.
    – iceKing
    Commented Apr 25, 2014 at 8:45
  • I have another method, but I don't know why. I copy the A.so to build folder, and add the flags "-Wl,-rpath=." then it will compile successfully.
    – iceKing
    Commented Apr 25, 2014 at 8:46
  • @iceKing If you want to know, check out the explanation of -rpath option in ld man page.
    – Lee Duhem
    Commented Apr 25, 2014 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.