CMake:重用为 lib 构建的目标文件到另一个 lib 目标中 [英] CMake: reuse object files built for a lib into another lib target

查看:28
本文介绍了CMake:重用为 lib 构建的目标文件到另一个 lib 目标中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目移至 CMake,同时对编译过程进行一些优化.

交易如下:

  • 我有几个子目录(必须),每个子目录都编译成一个静态库(这是可行的).
  • 我想将每个子目录中的所有目标文件收集到另一个更大、更完整的静态库中.

看起来像这样:

<预><代码>.libBig.a # 由来自 subdir1 和 subdir2 的对象制成子目录1/来源/libSubdir1.a子目录2/来源/libSubdir2.a

今天,我设法使用了一个全局变量,其中每个子目录 CMakeLists.txt 都将附加自己的源文件.我在我的大库中使用这个变量作为源"输入:

#大库依赖所有源文件# ${all_src} 自动填充每个子目录的cpp文件get_property(BigLib_src GLOBAL PROPERTY all_src)add_library( Big STATIC ${BigLib_src}) # 重新编译所有源

现在,这行得通,还不错,但问题是,我所有的源文件都编译了两次:一次用于 subdir 库,一次用于大库.

CMake 似乎忘记了它已经构建了它们.

必须保留子目录库并且ar不能合并两个静态库.

你知道怎么做吗?

解决方案

您可以使用 CMake 2.8.8.此处解释了这个想法.基本上,OBJECT 库是一个类似于 Autotools 已知的 便利库的概念,用于对目标文件进行分组.

查看完整的 CMake OBJECT 库教程.

I'm trying to move my project to CMake, and at the same time have some optimization on the compilation process.

Here's the deal:

  • I have several subdirs that are (have to be) each compiled into a static library (this works).
  • I want to gather all the object files from each subdir into another bigger, complete, static library.

It looks like this:

.
libBig.a  # made from object from subdir1 and subdir2
subdir1/
   src/
   libSubdir1.a
subdir2/
   src/
   libSubdir2.a

Today, I managed to use a global variable in which every subdir CMakeLists.txt will append its own source files. I use this variable as a "source" input in my big library:

# the big library depends on all the source files
# ${all_src} is automatically filled with each subdir's cpp file
get_property( BigLib_src GLOBAL PROPERTY all_src)
add_library( Big STATIC ${BigLib_src}) # recompiles all the sources

Now, this works, not too bad, but the thing is, all my source files get compiled twice: once for the subdir library, and once for the big library.

CMake seems to forget that it has already built them.

I have to keep the subdir libraries and ar can't merge two static libraries.

Do you know how to do that?

解决方案

You can use the new OBJECT library feature introduced in CMake 2.8.8. The idea is explained here. Basically, the OBJECT library is a similar concept to the convenience library known from Autotools to group object files.

Check the complete CMake OBJECT library tutorial.

这篇关于CMake:重用为 lib 构建的目标文件到另一个 lib 目标中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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