bag*_*age 4 bash shell arguments cmake
我正在努力解决一个奇怪的问题。我正在尝试使用 shell 变量作为参数运行 cmake 命令行,但它失败了。这就是我所做的:
#1. Basic. works fine
cmake -G 'Sublime Text 2 - Ninja'
#2. Argument into variable. error
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake $CMAKE_CONFIG ../..
> CMake Error: Could not create named generator 'Sublime Text 2 - Ninja'
#3. Adding -v before variable. 'compile' but ignore the argument (generate a Makefile). Hacky and senseless?
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake -v$CMAKE_CONFIG ../..
#4. Quoting argument. error (same as #2)
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake "$CMAKE_CONFIG" ../..
Run Code Online (Sandbox Code Playgroud)
使用 --trace 和 --debug-output 变量给出以下结果:
#5. Working command
cmake ../.. --trace --debug-output -G "Sublime Text 2 - Ninja"
#6. Non existing generator.
#Expected result (witness purpose only)
cmake ../.. --trace --debug-output -G 'random test'
[...]
CMake Error: Could not create named generator random test
#7. Testing with variable.
#Output error quotes the generator's name and there is an extra space before it
cmake ../.. --trace --debug-output $CMAKE_CONFIG
[...]
CMake Error: Could not create named generator 'Sublime Text 2 - Ninja'
#8. Removing the quote within the variable.
#Still error, but the only difference with #6 is the extra space after 'generator'
CMAKE_CONFIG="-G Sublime Text 2 - Ninja"
cmake ../.. --trace --debug-output $CMAKE_CONFIG
[...]
CMake Error: Could not create named generator Sublime Text 2 - Ninja
Run Code Online (Sandbox Code Playgroud)
我也尝试更改 IFS 变量,但没有成功实现我的目标。
有什么提示吗?
在这种情况下,您需要调试 shell,而不是cmake. printf '%q\n'诀窍是在命令中替换“cmake” ,以向bash您展示它如何解释您的参数。
我认为使用这样的数组会起作用:
CMAKE_CONFIG=(-G 'Sublime Text 2 - Ninja')
cmake "${CMAKE_CONFIG[@]}" ../..
Run Code Online (Sandbox Code Playgroud)