Pruning a static library in c++

半城伤御伤魂 提交于 2020-01-06 08:33:14

问题


I am trying to expose a single well defined class by building a static library and then shipping the built library with a few header files that define that class and the interfaces needed to use it. I have that working but the problem I am running into is the library is gigantic. It has every single object file from the whole project and all I need is a subset. If I make a simple main.cpp file and include and use that single class then I get a output file that is only as big as the 20% of the library I am using. Is there a way to tell the linker to start from a given place and prune everything else like in the executable case?

EDIT: I forgot to mention that I am using gcc on cygwin and linux (though I would like a solution that worked with visual studio as well, we generally use that for development but deploy primarily on linux)


回答1:


Make a shared library. It behaves like an executable from the point of view of linking etc. It should do the discarding you mentioned you saw on the executable.




回答2:


You have to split the project up. Take out the files needed for the library and make that a separate project just building the lib.

The remaining project (with main.cpp in) needs to call the new lib project to get the lib Details depend on what tools and OS you are using to manage the project (e.g. Visual Studio or make or ... )




回答3:


You haven't told us what toolchain you are using but since you say project, I'm guessing you are using the MS toolchain.

The MS toolchain includes every object from a project into the static library. What you want to do is break your single class into a separate project. You can continue to have a super-project that includes that class so you don't have to modify any of your existing projects.

Now, if you want to take this to the next level, you should consider putting each member of the class into it's own translation unit (i.e. .cpp file). This way, if a user of the class only needs a few parts of the class, they will only need to link in the parts they need.



来源:https://stackoverflow.com/questions/1677585/pruning-a-static-library-in-c

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