问题描述
我正在尝试将 CMake 构建到目录build"中,如在 project/build
中一样,其中 CMakeLists.txt 位于 project/
中.
I'm trying to get CMake to build into a directory 'build', as in project/build
, where the CMakeLists.txt is in project/
.
我知道我能做到:
mkdir build
cd build
cmake ../
但这很麻烦.我可以将它放在一个脚本中并调用它,但是为 CMake 提供不同的参数(如 -G "MSYS Makefiles")很不愉快,或者我需要在每个平台上编辑这个文件.
but that is cumbersome. I could put it in a script and call it, but then it's unpleasant to provide different arguments to CMake (like -G "MSYS Makefiles"), or I would need to edit this file on each platform.
我最好在主 CMakeLists.txt 中执行类似 SET(CMAKE_OUTPUT_DIR build)
的操作.请告诉我这是可能的,如果是这样,如何?或者其他一些源代码构建方法可以轻松指定不同的参数?
Preferably I would do something like SET(CMAKE_OUTPUT_DIR build)
in the main CMakeLists.txt. Please tell me that this is possible, and if so, how? Or some other out of source build method that makes it easy to specify different arguments?
推荐答案
CMake 3.13 或更新版本支持 命令行选项 -S
和 -B
分别指定源目录和二进制目录.
CMake 3.13 or newer supports the command line options -S
and -B
to specify source and binary directory, respectively.
cmake -S . -B build -G "MSYS Makefiles"
这将在当前文件夹中查找 CMakeLists.txt
并在其中创建一个 build
文件夹(如果尚不存在).
This will look for the CMakeLists.txt
in the current folder and create a build
folder (if it does not yet exist) in it.
对于旧版本的 CMake,您可以使用未公开的 CMake 选项 -H
和 -B
在调用 cmake代码>:
For older versions of CMake, you can use the undocumented CMake options -H
and -B
to specify the source and binary directory upon invoking cmake
:
cmake -H. -Bbuild -G "MSYS Makefiles"
注意选项和目录路径之间不能有空格字符.
Note that there must not be a space character between the option and the directory path.
这篇关于让 CMake 在不包装脚本的情况下构建源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!