CMake 究竟是如何工作的? [英] How exactly does CMake work?

查看:30
本文介绍了CMake 究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不只是为了我自己问这个.我希望这个问题能成为许多和我一样的新手的参考,因为对于这么小的 CMakeLists.txt 文件

I'm not asking this for just myself. I hope this question will be a reference for the many newbies who like me, found it utterly perplexing about what exactly what was going on behind the scenes when for such a small CMakeLists.txt file

cmake_minimum_required (VERSION 2.6)
project(Tutorial)
add_executable(Tutorial tutorial.cpp)

还有这么小的tutorial.cpp

int main() { return 0; } 

生成的文件太多了

CMakeCache.txt  cmake_install.cmake  Makefile
CMakeLists.txt  tutorial.cpp

和一个CMakeFiles文件夹,里面有很多文件和文件夹

and a CMakeFiles folder with so many files and folders

CMakeCCompiler.cmake               CMakeOutput.log    Makefile.cmake
cmake.check_cache                  CMakeSystem.cmake  progress.marks
CMakeCXXCompiler.cmake             CMakeTmp           TargetDirectories.txt
CMakeDetermineCompilerABI_C.bin    CompilerIdC        Tutorial.dir
CMakeDetermineCompilerABI_CXX.bin  CompilerIdCXX
CMakeDirectoryInformation.cmake    Makefile2

不了解幕后发生的事情(即:为什么必须生成文件以及它们的目的是什么),是学习 CMake 的最大障碍.

Not understanding what was going on behind the scenes (i.e: why so may files had to be generated and what their purpose was), was the biggest obstacle in being able to learn CMake.

如果有人知道,可以为后人解释一下吗?这些文件的用途是什么,当我输入 cmake . 时,cmake 在构建项目之前配置和生成的究竟是什么?

If anyone knows, could you please explain it for the sake of posterity? What is the purpose of these files, and when I type cmake ., what exactly is cmake configuring and generating before it builds the project?

推荐答案

秘诀在于您不必了解生成的文件的用途.

The secret is that you don't have to understand what the generated files do.

CMake 在构建系统中引入了很多复杂性,其中大部分只有在您将其用于构建复杂的软件项目时才会得到回报.

CMake introduces a lot of complexity into the build system, most of which only pays off if you use it for building complex software projects.

好消息是 CMake 做得很好,让你远离这种混乱:使用 源外构建,您甚至不必查看生成的文件.如果到目前为止您还没有这样做(我猜是这种情况,因为您编写了 cmake .),请在继续之前检查它们.混合构建和源目录对于 CMake 来说真的很痛苦,而不是应该如何使用系统.

The good news is that CMake does a good job of keeping a lot of this messiness away from you: Use out-of-source builds and you don't even have to look at the generated files. If you didn't do this so far (which I guess is the case, since you wrote cmake .), please check them out before proceeding. Mixing the build and source directory is really painful with CMake and is not how the system is supposed to be used.

简而言之:代替

cd <source_dir>
cmake .

一直使用

cd <build_dir_different_from_source_dir>
cmake <source_dir>

我通常在我的源目录中使用一个空的子文件夹 build 作为构建目录.

I usually use an empty subfolder build inside my source directory as build directory.

为了减轻您的痛苦,让我快速概述一下 CMake 生成的相关文件:

To ease your pain, let me give a quick overview of the relevant files which CMake generates:

  • 项目文件/Makefiles - 您真正感兴趣的内容:在所选生成器下构建项目所需的文件.这可以是从 Unix Makefile 到 Visual Studio 解决方案的任何内容.
  • CMakeCache.txt - 这是一个持久的键/值字符串存储,用于在运行之间缓存值.存储在此处的值可以是库依赖项的路径,也可以是是否要构建可选组件.变量列表与您在运行 ccmakecmake-gui 时看到的列表大致相同.不时查看这可能很有用,但我建议尽可能使用上述工具更改任何值.
  • 生成的文件 - 这可以是从自动生成的源文件到导出宏的任何内容,可帮助您将构建的项目与其他 CMake 项目重新集成.其中大部分仅按需生成,不会出现在您问题中的简单项目中.
  • 其他任何事情 几乎都是让构建系统满意的噪音.特别是,我从不需要关心 CMakeFiles 子目录中发生的任何事情.
  • Project files/Makefiles - What you are actually interested in: The files required to build your project under the selected generator. This can be anything from a Unix Makefile to a Visual Studio solution.
  • CMakeCache.txt - This is a persistent key/value string storage which is used to cache value between runs. Values stored in here can be paths to library dependencies or whether an optional component is to be built at all. The list of variables is mostly identical to the one you see when running ccmake or cmake-gui. This can be useful to look at from time to time, but I would recommend to use the aforementioned tools for changing any of the values if possible.
  • Generated files - This can be anything from autogenerated source files to export macros that help you re-integrate your built project with other CMake projects. Most of these are only generated on demand and will not appear in a simple project such as the one from your question.
  • Anything else is pretty much noise to keep the build system happy. In particular, I never needed to care about anything that is going on inside the CMakeFiles subdirectory.

一般来说,您不应该弄乱 CMake 为您生成的任何文件.所有问题都可以在 CMakeLists.txt 中以一种或另一种方式解决.只要结果按预期构建您的项目,您就可能没问题.不要太担心血腥的细节 - 因为这就是 CMake 一开始就试图让你省心的事情.

In general you should not mess with any of the files that CMake generates for you. All problems can be solved from within CMakeLists.txt in one way or the other. As long as the result builds your project as expected, you are probably fine. Do not worry too much about the gory details - as this is what CMake was trying to spare you of in the first place.

这篇关于CMake 究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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