How to pass a variable from command line to a CMake Toolhain file?

前提是你 提交于 2020-07-19 11:12:46

问题


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 .


回答1:


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:

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

CMakeLists.txt empty

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

Output is:

Toolchain file loaded with path: /path

The order of <src-path> and -DCMAKE_TOOLCHAIN_FILE=./ToolChain.cmake is important.

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

doesn't work.



来源:https://stackoverflow.com/questions/52071882/how-to-pass-a-variable-from-command-line-to-a-cmake-toolhain-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!