CMake:将为 lib 构建的对象文件重用到另一个 lib 目标中

发布于 2024-10-19 09:05:45 字数 833 浏览 2 评论 0原文

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

事情是这样的:

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

它看起来像这样:

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

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

# 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

现在,这有效,还不错,但问题是,我的所有源文件都被编译了两次:一次用于子目录库,还有一次去大图书馆。

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

必须保留子目录库,并且ar无法合并两个静态库。

你知道该怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-10-26 09:05:45

您可以使用 CMake 2.8.8 中引入的新 OBJECT 库功能。这个想法在此处进行了解释。基本上,OBJECT 库与 Autotools 中已知的便利库类似。组对象文件。

查看完整的 CMake OBJECT 库教程

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.

放低过去 2024-10-26 09:05:45

As of CMake 2.8.8, you can do this using the OBJECT library type. See mloksot's answer. The old situation was that each target had its own directory and CMake would build every dependent object for every target. This guards against a case where one source file could be used multiple times with different CFLAGS. (Note that by default CMake is more conservative than automake here: automake will stop reusing object files for different targets only if the targets are built with different CFLAGS (and probably CPPFLAGS, too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文