如何将变量从命令行传递到CMake Toolhain文件? [英] How to pass a variable from command line to a CMake Toolhain file?

查看:136
本文介绍了如何将变量从命令行传递到CMake Toolhain文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用cmake时是否可以将变量传递给工具链文件?

Is there a way to pass a variable to a toolchain file when invoking cmake?

例如,我有以下工具链文件:

For example, I have the following toolchain file:

message("FOO = ${FOO}")

我尝试了以下方法,但是没有用.该变量未在工具链文件中设置.

I have tried the following, but it did not work. The variable is not set in the toolchain file.

cmake <src-dir> -DCMAKE_TOOLCHAIN_FILE=<toolchain-file> -DFOO="bar" -B <build-dir>


我实际上想要实现的是将路径传递到工具链文件.而且我无法在.


What I am actually trying to achieve is passing a path to the toolchain file. And I can't modify the main CMakeLists.txt in .

推荐答案

您必须使用 -DCMAKE_TOOLCHAIN_FILE 设置工具链文件的路径.以下对我有用(cmake3版本3.12.0):

You have to set the path to your toolchain file with -DCMAKE_TOOLCHAIN_FILE. The following works for me (cmake3 version 3.12.0):

ToolChain.cmake:

ToolChain.cmake:

message("Toolchain file loaded with path: ${FOO}")

CMakeLists.txt为空

CMakeLists.txt empty

cmake -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake . -DFOO=/path

输出为:

Toolchain file loaded with path: /path

< src-path> -DCMAKE_TOOLCHAIN_FILE =./ToolChain.cmake 的顺序很重要.

cmake . -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake -DFOO=/path

不起作用.

这篇关于如何将变量从命令行传递到CMake Toolhain文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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