提取大型库的小部分(fx boost)
我想知道是否有一种自动方法来提取大型 C++ 库的一小部分。
假设我在某些项目中只需要 boost::rational 。然而整个 boost 1.42 占用了 279 MiB!
为了保持我的项目“独立”(用于一些学校作业),我希望能够包含 boost::rational 以及我自己的源代码。 (这个想法是,我的老师不应该为了编译而提前安装 1000 个库)
我知道这违反了良好实践,因为最好安装整个 boost - 但这个论点仍然适用于其他(鲜为人知)大型图书馆。
我想通过遍历根 #include 的 #include 依赖树(如 boost/rational.hpp)可以轻松完成此提取;但这样的工具已经制造出来了吗?它叫什么名字?
I would like to know if there is an automated way to extract a small portion of a large C++ library.
Let's say I only need boost::rational in some project. However entire boost 1.42 takes up 279 MiB!
To keep my project "self-contained" (fx for some school work), I would like to be able to include boost::rational along with my own source. (The idea being, that my teacher should not have to install 1000's of libraries in advance in order to compile)
I know this violates good practice, as it would be better to actually have entire boost installed - but the argument nevertheless holds with other (lesser know) large libraries.
I guess this extraction could be done easily by walking the #include dependency tree of the root #include (like boost/rational.hpp); but has such a tool been made? What's its name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Linux 下,您可以使用 "x" 标记 "ar" 从库中提取所有目标文件。
您可以使用“nm”来确定代码需要哪些符号,以及哪些(库)目标文件定义它们。 (有一个可选的 --demangle 标志,这可能有助于人们阅读输出。)
然后您可以构建一个仅包含您需要的目标文件的新库。 (通过“ar”和“ranlib”。)或者直接在命令行上编译(链接)它们。
只需编写脚本即可找到(编译的)目标代码中缺少的符号,然后找到库中的哪些目标文件定义它们。当然,那些需要其他库文件的库对象文件中缺少哪些符号...以及这些新库(对象)文件中缺少的符号...等等。等等。
归根结底,就是付出了大量的努力,但收获却(通常)太少。特别是当你遇到弱符号、间接引用等问题时。
Under Linux you can use the "x" flag to "ar" to extract all the object files from a library.
You can use "nm" to determine what symbols are needed by your code, and which (library) object files define them. (There is an optional --demangle flag, which might help human's reading the output.)
You could then build a new library consisting of just the object files you needed. (Via "ar" and "ranlib".) Or just compile (link) them in directly on the command line.
It's a simple matter of scripting to find the symbols missing from your (compiled) object code, and then which object files from the library define them. And then of course what symbols are missing from those library object files that require other library files... And the ones that are missing from these new library (object) files... And so on. And so on.
It boils down to a lot of work for (usually) far too little gain. Especially when you get into things like Weak Symbols, Indirect References, etc.