It feels strange to me to use <span>-Wl,-Bstatic</span> in order to tell <span>gcc</span> which libraries I want to link with statically. After all, I’m telling gcc directly all other information about linking with libraries (<span>-Ldir, -llibname</span>).
Is it possible to tell the <span>gcc</span> driver directly which libraries should be linked statically?
Clarification: I know that if a certain library exists only in static versions it’ll use it without<span>-Wl,-Bstatic</span>, but I want to imply gcc to prefer the static library. I also know that specifying the library file directly would link with it, but I prefer to keep the semantic for including static and dynamic libraries the same.
Use <span>-l:</span> instead of <span>-l</span>. For example, <span>-l:libXYZ.a</span> to link with <span>libXYZ.a</span>. Notice the <span>lib</span> and <span>.a</span> are written out, as opposed to <span>-lXYZ</span> which would auto-expand to <span>libXYZ.so/libXYZ.a</span>.
It is an option of the GNU <span>ld</span> linker:
<span>-l namespec</span>… If<span>namespec</span>is of the form<span>:filename</span>,<span>ld</span>will search the library path for a file called<span>filename</span>, otherwise it will search the library path for a file called<span>libnamespec.a</span>. … on ELF … systems,<span>ld</span>will search a directory for a library called<span>libnamespec.so</span>before searching for one called<span>libnamespec.a</span>. … Note that this behavior does not apply to<span>:filename</span>, which always specifies a file called<span>filename</span>.“
Note that this only works with the GNU linker. If your ld isn’t the GNU one you’re out of luck.
References
- https://stackoverflow.com/questions/6578484/telling-gcc-directly-to-link-a-library-statically
- https://sourceware.org/binutils/docs/ld/Options.html
Give us a “like” + “view” ❤️ to let us know this text has warmed you, and it is also our greatest motivation for continuous creation!