Extracting small parts of large library (fx boost)

倖福魔咒の 提交于 2020-01-15 09:06:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4119726/extracting-small-parts-of-large-library-fx-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!