11

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

Community
  • 1
  • 1
S B
  • 8,134
  • 10
  • 54
  • 108
  • 1
    I am getting this `ld: warning: unexpected dylib (/path/to/libfoo.dylib) on link line`. There is also this .a in that same location. I manually deleted the dylib, & now `ld` is forced to pick up .a. But that should not be the solution because `-Wl,-static` is supposed to do the disambiguation, not me – S B Jan 18 '12 at 13:57

2 Answers2

6

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.

Community
  • 1
  • 1
Adiel Mittmann
  • 1,764
  • 9
  • 12
  • $ ld -v @(#)PROGRAM:ld PROJECT:ld64-127.2 llvm version 3.0svn, from Apple Clang 3.0 (build 211.10.1) – S B Jan 18 '12 at 13:42
  • +1 Thanks for the explanation! Is there a way to pick up a non-Apple linker then? – S B Jan 18 '12 at 14:05
  • Looks like it's not possible to use the GNU linker ([How to install gnu ld on mac os x 10.6?](http://stackoverflow.com/questions/1608009/how-to-install-gnu-ld-on-mac-os-x-10-6)), which basically indicates that you have to move files around to mix shared and static libraries :S – Adiel Mittmann Jan 18 '12 at 16:30
  • @SaptarshiBiswas using a "proxy" `lib` directory wih symlinks might be more flexible. It's still a user-side hack though. – rubenvb Jun 27 '13 at 09:44
2

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.

U007D
  • 5,772
  • 2
  • 38
  • 44