链接时可以混合使用静态库和共享对象库吗? [英] Can I mix static and shared-object libraries when linking?

查看:29
本文介绍了链接时可以混合使用静态库和共享对象库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C 项目,可以生成十个可执行文件,我想静态链接所有这些文件.我面临的问题是这些可执行文件之一使用了第 3 方库,其中只有共享对象版本可用.

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.

如果我将 -static 标志传递给 gcc,ld 会出错,说它找不到相关的库(我认为它正在寻找 .a 版本)并且可执行文件不会建成.理想情况下,我希望能够告诉ld"尽可能多地静态链接,如果找不到静态库,则故障转移到共享对象库.

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.

在 interium 中,我尝试了类似 gcc -static -lib1 -lib2 -shared -lib3rdparty foo.c -o foo.exe 之类的东西,希望 'ld' 会在 lib1 和 lib2 中静态链接,但是只对 lib3rdparty 有运行时依赖.不幸的是,这并没有达到我的预期.相反,-shared 标志覆盖了 -static 标志,所有东西都被编译为共享对象.

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.

是静态链接一个全有或全无的交易,还是有某种方法可以混合搭配?

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

推荐答案

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

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

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

这篇关于链接时可以混合使用静态库和共享对象库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆