使CMake在不包装脚本的情况下从源代码构建 [英] Getting CMake to build out of source without wrapping scripts

查看:56
本文介绍了使CMake在不包装脚本的情况下从源代码构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让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在不包装脚本的情况下从源代码构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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