I have a C++ based project(many source files) compiled using gnu make via a makefile. I have an application which links a library, say mylib. Now mylib is owned by some other developer. I see 2 files present in the path where the library binaries are generated namely libmylib.so (shared object) and libmylib.a (static library archive file)

My application makefile has below linker option to link the library mylib

LDFLAGS+=-l:mylib ...

Question is what version of the library mylib would be linked in my application executable

Would the shared object libmylib.so or static version libmylib.a ?

How would it be decided, would there be any other makefile option to govern this?

share|improve this question
1  
I think it depends on the platform/compiler, but IIRC gcc defaults to dynamic unless -static is specified. – user529758 Feb 6 '13 at 14:22
up vote 4 down vote accepted

By default on non embedded Linux, you'll get dynamic linkage. If you want to change that, you can specify it in LDFLAGS;

LDFLAGS+= -Wl,--Bstatic -lmylib -Wl,--Bdynamic

(possibly quotes are required) This will switch to static for this lib only, then back to the default dynamic.

share|improve this answer
    
Thanks. I will wait for a day to see if any other answer pops in before accepting. – goldenmean Feb 6 '13 at 14:56
    
Just a query man ld mentions it as -Bstatic (--Bdynamic) instead of --Bstatic(--Bdynamic). What is it actually? – goldenmean Feb 6 '13 at 16:04
    
@goldenmean Both seem to work for this option on Linux actually, but you're right, the documentation seems to favor -Bstatic. – Joachim Isaksson Feb 6 '13 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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