My question is an extension of this question
I want to link against 2 libraries - foo
and bar
preferring static for foo and dynamic for bar. If I use
g++ -static -lfoo -lbar
it tries to find static archives for both foo and bar. When I change the command to
g++ -Wl,-Bstatic -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed
as per the above SO question, this is the error I get:
ld: unknown option: -Bstatic
Update: I am using OSX, if that makes any difference
Yes, unfortunately, using OS X is making the difference. -static
is asking the compiler to give you a fully statically linked executable (not supported on OS X), and as Adiel pointed out, -Wl,-Bstatic for mixing static and dynamic linking isn't supported by Apple's clang linker.
To get around this problem on the Mac try:
g++ myapp.cpp libfoo.a libbar.a
as your compile line (where library names follow your source on the command line). This will give you myapp statically linked with the foo and bar libraries, while other required libraries will be linked in dynamically.
Are we dealing with the GNU linker here? Can you show us the output of "ld -v"?
EDIT: that doesn't look like GNU's ld, so that's why the -Bstatic option is not recognized. And it seems that Apple's ld doesn't support mixing static and dynamic libraries very well; see this: Mixed static and dynamic link on Mac OS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With