Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make gcc ignore static libraries when linking shared libraries?

I've encountered a few cases building projects which use shared libraries or dynamic-loaded modules where the module/library depends on another library, but doesn't check that a shared copy is available before trying to link. This causes object files from a static archive (.a file) to get pulled into the resulting .so, and since these object files are non-PIC, the resulting .so file either has TEXTRELs (very bad load performance and memory usage) or fails altogether (on archs like x86_64 that don't support non-PIC shared libraries).

Is there any way I can make the gcc compiler driver refuse to link static library code into shared library output? It seems difficult and complicated by the possible need to link minimal amounts from libgcc.a and the like...

like image 941
R.. GitHub STOP HELPING ICE Avatar asked Nov 14 '22 20:11

R.. GitHub STOP HELPING ICE


1 Answers

As you know, you can use -static to only link against static libraries, but there doesn't appear to be a good equivalent to only linking against dynamic libraries.

The following answer may be useful...

How to link using GCC without -l nor hardcoding path for a library that does not follow the libNAME.so naming convention?

You can use -l:[libraryname].so to list the dynamic libraries you want to link against in your library search path. Specifying the .so ending will probably help with your dynamic library only case. You will probably have to specify the whole name with the 'lib' prefix instead of just the shortened version.

like image 140
ricosrealm Avatar answered Jan 08 '23 08:01

ricosrealm