What library would it link against - static or sha

2019-07-30 15:37发布

问题:

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?

回答1:

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.