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

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

问题描述

我有一个产生10可执行一个C项目,所有这一切,我想静态链接英寸我面临的问题是,这些可执行文件之一使用第三方库,其中只有共享对象的版本。

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,劳工处会报错说无法找到问题的库(I presume它寻找。一个版本)和可执行文件将不被构建。理想情况下,我希望能够告诉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天全站免登陆