CMake-将多个库合并为一个 [英] CMake - combine multiple libraries into one

查看:233
本文介绍了CMake-将多个库合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有可执行文件A,B,C,并且我具有外部库X,Y,Z

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})

但是,如果我将其可视化(使用 cmake --graphviz 选项,则会得到一个复杂的二部图,其边缘从每个可执行文件A,B和C到每个库X,Y和Z.

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.

推荐答案

所有这些都取决于您的平台,编译器和库的类型:

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

  1. 以防万一,您可以自己构建X,Y,Z:创建一个新项目 XYZ ,该项目是根据X,Y和Z的汇总源文件构建的.是可能的,您可能不会在SO上询问过.

  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.

如果您无法重建这些库,并且它们是作为共享库(dll/so)构建的,那么您很不走运.您可以尝试编写一个包装器库,该包装器隐藏X,Y和Z的所有内部结构,并将由应用程序A,B和C使用.

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.

如果它们是作为静态库构建的,请查看此SO问题.由于静态库只不过是目标文件的存档,因此您可以从每个库中提取目标代码,并用 ar 对其进行重新组合.

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.

但是你为什么呢?假设您获得了仅依赖X的第四个应用程序D.无论如何,您都将需要单独的库Y(除非您不想与所有库不必要地链接).

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).

这篇关于CMake-将多个库合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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