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

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

问题描述

我试图将我的项目移动到CMake,同时对编译过程进行一些优化。



这是协议:




  • 我有几个子文件(必须)被编译成静态库(这个工作)。

  • 我想将每个子目录中的所有对象文件收集到另一个更大,完整,静态的库中。



/ p>

 
libBig.a#from subdir1和subdir2的对象
subdir1 /
src /
libSubdir1.a
subdir2 /
src /
libSubdir2.a

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

 #大库取决于所有的源文件
#$ {all_src}自动填充每个subdir的cpp文件
get_property(BigLib_src GLOBAL PROPERTY all_src)
add_library(Big STATIC $ {BigLib_src})#重新编译所有源

现在,这不错,但事情是,所有我的源文件被编译两次:一次用于subdir库,一次用于大图书馆。



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



我保留subdir程式库, ar 无法合并两个静态程式库。



你知道如何做吗?

解决方案

您可以使用新的OBJECT库功能在 CMake 2.8.8 中介绍。 此处解释了这个想法。基本上,OBJECT库与从Autotools已知的便利库类似的概念,用于将对象文件分组。 / p>

检查完整的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天全站免登陆