在cmake中,什么是“项目”? [英] In cmake, what is a "project"?

查看:136
本文介绍了在cmake中,什么是“项目”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于 project 命令的,并扩展了 project 的概念。 c $ c>表示cmake。我真的不明白什么是项目,它与目标有什么不同(我知道,我认为)。

This question is about the project command and, by extension, what the concept of a project means in cmake. I genuinely don't understand what a project is, and how it differs from a target (which I do understand, I think).

我看了 project 命令的cmake文档,它表示 project 命令这样做:


设置整个项目的名称,版本并启用语言。

Set a name, version, and enable languages for the entire project.

不言而喻,使用单词 project 定义 project 并没有帮助。

It should go without saying that using the word project to define project is less than helpful.

页面上的任何地方似乎都没有解释什么项目实际上是 (它通过了命令进行的某些操作,但是不说该列表是否是排他性的)。 cmake.org示例指导我们完成基本的构建设置,同时使用 project 关键字,它也不能解释其作用或含义,至少据我所知。

Nowhere on the page does it seem to explain what a project actually is (it goes through some of the things the command does, but doesn't say whether that list is exclusive or not). The cmake.org examples take us through a basic build setup, and while it uses the project keyword it also doesn't explain what it does or means, at least not as far as I can tell.

什么是项目 project 命令有什么作用?

What is a project? And what does the project command do?

推荐答案

一个项目在逻辑上进行分组

A project logically groups a number of targets (that is, libraries, executables and custom build steps) into a self-contained collection that can be built on its own.

在实践中,这意味着,许多目标(即库,可执行文件和自定义构建步骤)都可以包含到一个独立的集合中。

如果在 CMakeLists.txt 中具有 project 命令,则应该能够从该文件运行CMake,并且生成器应生成可构建的东西。在大多数代码库中,每次构建都只有一个项目。

In practice that means, if you have a project command in a CMakeLists.txt, you should be able to run CMake from that file and the generator should produce something that is buildable. In most codebases, you will only have a single project per build.

但是请注意,您可以嵌套多个项目。顶级项目可能包含一个子目录,该子目录又是另一个自包含项目。在这种情况下, project 命令引入了某些值的其他作用域。例如, PROJECT_BINARY_DIR 变量将始终指向当前项目的根二进制目录。将其与 CMAKE_BINARY_DIR ,它始终指向顶层项目的二进制目录。另请注意,某些生成器可能会为项目生成其他文件。例如,Visual Studio生成器将为每个子项目创建一个 .sln 解决方案文件。

Note however that you may nest multiple projects. A top-level project may include a subdirectory which is in turn another self-contained project. In this case, the project command introduces additional scoping for certain values. For example, the PROJECT_BINARY_DIR variable will always point to the root binary directory of the current project. Compare this with CMAKE_BINARY_DIR, which always points to the binary directory of the top-level project. Also note that certain generators may generate additional files for projects. For example, the Visual Studio generators will create a .sln solution file for each subproject.

使用子项目如果您的代码库非常复杂,并且您需要用户能够隔离地构建某些组件。这为您提供了构建构建系统的非常强大的机制。由于要使几个子项目真正独立起来需要增加编码和维护开销,因此我建议仅在有实际用例的情况下才这样做。将代码库分为不同的目标始终应该是构建构建的首选机制,而子项目应保留给那些确实需要使目标子集完全独立的罕见情况。

Use sub-projects if your codebase is very complex and you need users to be able to build certain components in isolation. This gives you a very powerful mechanism for structuring the build system. Due to the increased coding and maintenance overhead required to make the several sub-projects truly self-contained, I would advise to only go down that road if you have a real use case for it. Splitting the codebase into different targets should always be the preferred mechanism for structuring the build, while sub-projects should be reserved for those rare cases where you really need to make a subset of targets self-contained.

这篇关于在cmake中,什么是“项目”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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