CMake - combine multiple libraries into one

淺唱寂寞╮ 提交于 2021-01-27 09:22:32

问题


Let's say I have executables A, B, C, and I have external libraries X, Y, Z

SET(EXTERNAL_LIB X Y Z)
TARGET_LINK_LIBRARIES(A, ${EXTERNAL_LIB})
TARGET_LINK_LIBRARIES(B, ${EXTERNAL_LIB})
TARGET_LINK_LIBRARIES(C, ${EXTERNAL_LIB})

However, if I visualize this (using cmake --graphviz option, I get a complex bipartite graph with edges from each of the executables A, B, and C to each of the libraries X, Y, and Z.

I was wondering if there's a way to combine all the libraries into one.


回答1:


All of this depends a bit on your platform, compiler and the type of libraries:

  1. In case, you can build X, Y, Z yourself: Create a new project XYZ, built from the aggregated source files of X, Y and Z. But I guess if this was possible, you would not have asked on SO.

  2. If you can not rebuild the libs and they were built as a shared libraryy (dll/so), you are out of luck. You could try to write a wrapper library, which hides all the internals of X, Y and Z and which will be used by applications A, B and C.

  3. If they were build as static libs, take a look at this SO question. Because a static lib is not much more than an archive of object files, you may be able to extract the object code from each lib and recombine them with ar.

But why would you? Suppose you get a fourth application D, which only depends on X. Then you would need the separate library Y anyway (unless you prefer to unnecessarily link with all libs).



来源:https://stackoverflow.com/questions/6131730/cmake-combine-multiple-libraries-into-one

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