When linking against libraries using the -l
option (say -lfoo
), gcc will prefer a shared object to a static library if both are found (will prefer libfoo.so
to libfoo.a
). Is there a way to make gcc prefer the static library, if both are found?
The issue I'm trying to solve is the following: I'm creating a plugin for an application (the flight simulator called X-Plane), with the following constraints:
/usr/lib
or /usr/lib32
: LD_PRELOAD
or LD_LIBRARY_PATH
to find shared objects shipped with my pluginto solve the above constraints, a possible solution is to link the generated shared object against static, 32 bit versions of all non-trivial libraries used. but, when installing such libraries, usually both static and dynamic versions are installed, and thus gcc will always link against the shared object instead of the static library.
of course, moving / removing / deleting the shared objects in question, and just leaving the static libraries in say /usr/lib32
, is a work-around, but it is not a nice one
note:
-Wl,-static -lfoo -Wl,-Bdynamic,
but didn't bring the expected results-l:libfoo.a
as well, but this didn't bring the expected results eitherYou can't statically link a shared library (or dynamically link a static one). The flag -static will force the linker to use static libraries (. a) instead of shared (. so) ones.
They don't mix Things get even worse if you try to mix static and dynamic linking: you can end up with multiple copies of a library in the same app. Say your app uses two libraries: FooKit and BarKit, and they both depend on a third, libCore.
You can specify the full path to the static libs without the -l
flag to link with those.
gcc ... source.c ... /usr/lib32/libmysuperlib.a ...
Just add the .a
file to the link line without -l
as if it were a .o
file.
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