我可以在链接时混合静态和共享对象库吗?

时间:2022-02-21 20:11:29

I have a C project that produces ten executables, all of which I would like to be linked in statically. The problem I am facing is that one of these executables uses a 3rd-party library of which only the shared-object version is available.

我有一个生成10个可执行文件的C项目,所有这些我都希望静态地链接。我面临的问题是,其中一个可执行文件使用了一个第三方库,其中只有共享对象版本可用。

If I pass the -static flag to gcc, ld will error saying it can't find the library in question (I presume it's looking for the .a version) and the executable will not be built. Ideally, I would like to be able to tell 'ld' to statically link as much as it can and fail over to the shared object library if a static library cannot be found.

如果我将-static标志传递给gcc,那么ld将会错误地说它找不到有问题的库(我假设它正在寻找。a版本),并且可执行文件将不会被构建。理想情况下,我希望能够告诉‘ld’尽可能多地静态地链接,如果找不到静态库,则将失败转到共享对象库。

In the interium I tried something like gcc -static -lib1 -lib2 -shared -lib3rdparty foo.c -o foo.exe in hopes that 'ld' would statically link in lib1 and lib2 but only have a run-time dependence on lib3rdparty. Unfortunatly, this did not work as I intended; instead the -shared flag overwrote the -static flag and everything was compiled as shared-objects.

在interium中,我尝试过类似gcc -static -lib1 -lib2 -shared -lib3rdparty foo。c - o foo。exe希望“ld”会在lib1和lib2中静态链接,但只在运行时依赖于lib3rdparty。不幸的是,这并不如我所愿;相反,-shared标志覆盖了-static标志,所有内容都被编译为共享对象。

Is statically linking an all-or-nothing deal, or is there some way I can mix and match?

静态链接是“全有或全无”的交易,还是有什么方式可以混合和匹配?

1 个解决方案

#1


63  

Looking at this thread you can see that it can be done. The guys at GNU suggest

看看这个线程,你可以看到它是可以做到的。GNU的人建议。

gcc foo.c -Wl,-Bstatic -lbar -lbaz -lqux -Wl,-Bdynamic -lcorge -o foo.exe

#1


63  

Looking at this thread you can see that it can be done. The guys at GNU suggest

看看这个线程,你可以看到它是可以做到的。GNU的人建议。

gcc foo.c -Wl,-Bstatic -lbar -lbaz -lqux -Wl,-Bdynamic -lcorge -o foo.exe