Can I mix static and public libraries when linking?

I have a C project that creates ten executables, all of which I would like to link statically. The problem I am facing is that one of these executables uses a third-party library, from which only the version with shared objects is available.

If you pass the -static flag to gcc, ld will erroneously claim that it cannot find the library of interest (I assume that it is looking for the .a version) and the executable will not be created. Ideally, I would say 'ld' to statically link as many as I can, and move on to a shared library of objects if the static library cannot be found.

In interium, I tried something like gcc -static -lib1 -lib2 -shared -lib3rdparty foo.c -o foo.exe in the hope that 'ld' will statically link in lib1 and lib2, but will only have a dependency on lib3rdparty. Unfortunately, this did not work as I expected; instead, the -shared flag overwrites the -static flag, and everything was compiled as shared-objects.

Statically binds an all-or-nothing deal, or is there a way I can mix and match?

+46
c gcc static linker
Jun 02 2018-10-06T00:
source share
1 answer

Having looked at this stream , you can see that it can be done. GNU guys offer

 gcc foo.c -Wl,-Bstatic -lbar -lbaz -lqux -Wl,-Bdynamic -lcorge -o foo.exe 
+61
Jun 02 '10 at 1:17
source share



All Articles