CMake:如何最好地构建多个(可选)子项目? [英] CMake: how best to build multiple (optional) subprojects?

查看:635
本文介绍了CMake:如何最好地构建多个(可选)子项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个包含多个组件的总体项目:

Imagine an overall project with several components:


  • 基本

  • io

  • web

  • app-a

  • app-b

  • app-c

  • basic
  • io
  • web
  • app-a
  • app-b
  • app-c

现在,假设网络依赖于io,而io则依赖于基本的东西,所有这些东西都在一个仓库中,并且有一个CMakeLists.txt将它们构建为共享库。

Now, let's say web depends on io which depends on basic, and all those things are in one repo and have a CMakeLists.txt to build them as shared libraries.

如何设置内容,以便可以构建三个应用程序(如果每个应用程序都是可选的,可能不是)

一个想法是在主存储库中有一个空的 apps目录,我们可以将所需的任何应用程序存储库克隆到该存储库中。我们的主要CMakeLists.txt文件可以使用GLOB查找所有应用程序目录并构建它们(不知道会有多少个)。这种方法的问题包括:

One idea is to have an empty "apps" directory in the main repo and we can clone whichever app repos we want into that. Our main CMakeLists.txt file can use GLOB to find all the app directories and build them (not knowing in advance how many there will be). Issues with this approach include:


  • 显然,当您只说 make ,因此,如果您添加新应用,则必须再次运行 cmake

  • 这会给用户带来特定的结构

  • 不清楚如何创建一个应用程序的两个克隆并将它们分别针对同一库构建进行构建。

  • Apparently CMake doesn't re-glob when you just say make, so if you add a new app you must run cmake again.
  • It imposes a specific structure on the person doing the build.
  • It's not obvious how one could make two clones of a single app and build them both separately against the same library build.

一般概念类似于传统的递归CMake项目,但是较低级别的模块不一定事先知道哪个较高级别的模块将使用它们。但是,我不想让用户将较低级别的库安装在固定位置(例如 / usr / local / lib )。但是,我确实希望通过一次 make 调用来注意到整个项目中已更改的依赖关系,因此,如果我正在构建一个应用程序但已更改了一个低级库中的一个,一切都会正确地重新编译。

The general concept is like a traditional recursive CMake project, but where the lower-level modules don't necessarily know in advance which higher-level ones will be using them. Yet, I don't want to require the user to install the lower-level libraries in a fixed location (e.g. /usr/local/lib). I do however want a single invocation of make to notice changed dependencies across the entire project, so that if I'm building an app but have changed one of the low-level libraries, everything will recompile appropriately.

推荐答案

我最终完成了我在问题中概述的操作,即检入一个空目录(包含一个忽略所有内容的.gitignore文件),并将CMake的任何目录(由用户放在其中)告诉CMake。然后我可以说cmake myrootdir,它确实找到了所有各种组件。这或多或少行得通。但是,它确实有一些缺点,例如BuildBot之类的某些第三方工具希望使用更传统的项目结构,这使得将具有这种安排的其他工具集成起来需要更多的工作。

I ended up doing what I outlined in my question, which is to check in an empty directory (containing a .gitignore file which ignores everything) and tell CMake to GLOB any directories (which are put in there by the user). Then I can just say cmake myrootdir and it does find all the various components. This works more or less OK. It does have some side drawbacks though, such as that some third-party tools like BuildBot expect a more traditional project structure which makes integrating other tools with this sort of arrangement a little more work.

这篇关于CMake:如何最好地构建多个(可选)子项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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