问题
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