Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ linker does not recognize -Bstatic

Tags:

g++

linker

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

like image 224
S B Avatar asked Jan 18 '12 13:01

S B


2 Answers

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.

like image 97
U007D Avatar answered Oct 21 '22 01:10

U007D


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.

like image 6
Adiel Mittmann Avatar answered Oct 21 '22 00:10

Adiel Mittmann