我正在努力解决一个奇怪的问题。我试图使用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" ../..处理--跟踪和--调试输出变量会给出以下结果:
#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我也试图改变IFS变量,但没有成功实现我的目标。
有什么暗示吗?
发布于 2013-12-13 15:41:55
在这种情况下,您需要调试外壳,而不是cmake。诀窍是在命令中将"cmake“替换为printf '%q\n',让bash向您展示如何解释您的参数。
我认为使用这样的数组是可行的:
CMAKE_CONFIG=(-G 'Sublime Text 2 - Ninja')
cmake "${CMAKE_CONFIG[@]}" ../..https://stackoverflow.com/questions/20570042
复制相似问题