我可以让gcc链接器创建一个静态库吗? [英] Can I have the gcc linker create a static library?

查看:114
本文介绍了我可以让gcc链接器创建一个静态库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由大约300个c ++文件组成的库。



使用库的程序不想动态链接到它。 (出于各种原因,但最好的是,一些支持的平台不支持动态链接)。然后,我使用g ++和ar创建一个静态库(。)。 a)中,该文件包含所有这些文件的所有符号,包括库不想导出的文件。



我怀疑将消费程序与此库链接一个不必要的漫长时间,因为.a中的所有.o文件仍然需要解析它们的引用,并且链接器有更多的符号可供处理。



创建动态库(.dylib / .so)实际上可以使用链接器,它可以解析所有库内符号,并只导出库想要导出的链接器。但是结果只能在运行时被链接到消费程序中。



我想以某种方式获得动态链接的好处,但是使用静态库。 / p>

如果我的谷歌搜索是正确的,认为这确实是不可能的,我很想理解为什么这是不可能的,因为它看起来像许多c和c ++程序可以从中受益。

解决方案

我还没有试过或测试过这个,但它看起来像 ld 执行增量或部分链接的能力可能就是你想要的。检查 - relocatable 选项是否可用(如果处理C ++,您可能还需要查看 -Ur 选项)当应用到将进入库的目标文件将做你想要的。



我认为你应该能够使用该操作的输出作为一个对象文件(或者将其放在静态库本身中)作为程序的最终链接步骤。


I have a library consisting of some 300 c++ files.

The program that consumes the library does not want to dynamically link to it. (For various reasons, but the best one is that some of the supported platforms do not support dynamic linking)

Then I use g++ and ar to create a static library (.a), this file contains all symbols of all those files, including ones that the library doesn't want to export.

I suspect linking the consuming program with this library takes an unnecessary long time, as all the .o files inside the .a still need to have their references resolved, and the linker has more symbols to process.

When creating a dynamic library (.dylib / .so) you can actually use a linker, which can resolve all intra-lib symbols, and export only those that the library wants to export. The result however can only be "linked" into the consuming program at runtime.

I would like to somehow get the benefits of dynamic linking, but use a static library.

If my google searches are correct in thinking this is indeed not possible, I would love to understand why this is not possible, as it seems like something that many c and c++ programs could benefit from.

解决方案

I haven't tried or tested this, but it looks like ld's ability to perform incremental or partial linking might be what you're looking for. Check if the --relocatable option (you might also need to look at the -Ur option if dealing with C++) when applied to the object files that would go into the library will do what you want.

I think you should then be able to use the output of that operation as an object file (or have it in a static library itself) for your program's final link step.

这篇关于我可以让gcc链接器创建一个静态库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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