本文介绍了cmake在不同的构建目录中构建多个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下cmake结构

i have following cmake structure

CMakelists.txt
toolchain.cmake
folder1
   ----CMakelists.txt
folder2
   ----CMakelists.txt
etc..

我的第一级CMakelists.txt包括其他子目录。现在我想为不同的目标构建我的代码。

my first level CMakelists.txt includes the other subdirectorys. Now i want to build my code for different target.

手动方式是

1.
mkdir hostBuild
cd hostBuild
cmake ..
make
2.
mkdir crossBuild
cd crossBuild
cmake .. --DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
make

这个过程是否可以自动运行?

Is it possible that this process can run automatically?

例如,我只需要运行cmake。

For example i just have to run cmake . in my first level

是否可以这样?

推荐答案

否。建议只是将你的手动步骤放到一个shell脚本中。

No. The recommendation would be to just put your manual steps into a shell script.

CMake一次只能处理一个编译器/ make环境(如果你切换你需要的编译器一个不同的二进制输出目录)。

CMake can only handle one compiler/make environment at a time (if you switch the compiler you need a different binary output directory).

特别是包含 SET(CMAKE_SYSTEM_NAME ...)的工具链文件改变配置/生成过程的整个结果。

Especially a toolchain file that does contain SET(CMAKE_SYSTEM_NAME ...) does change the whole outcome of the configuration/generation process.

有关CMake看到的细节:

For details on what CMake does see: CMake: In which Order are Files parsed (Cache, Toolchain, …)?

您可以在shell脚本中使用一些CMake命令行选项:

And you could make use of some CMake command line options in your shell script:

if [ ! -d hostBuild ]; then
    cmake -E make_directory hostBuild
    cmake -E chdir hostBuild cmake ..
fi
cmake --build hostBuild
...

这篇关于cmake在不同的构建目录中构建多个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:45