使用bash变量参数调用cmake

时间:2013-12-13 15:25:48

标签: bash shell cmake arguments

我正在努力解决一个奇怪的问题。我试图以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" ../..

使用--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

我也试图改变IFS变量,但没有成功实现我的目标。

任何提示?

1 个答案:

答案 0 :(得分:3)

在这种情况下,您需要调试shell,而不是cmake。诀窍是在命令中用printf '%q\n'替换“cmake”,让bash向您展示它如何解释您的参数。

我认为使用这样的数组会起作用:

CMAKE_CONFIG=(-G 'Sublime Text 2 - Ninja')
cmake "${CMAKE_CONFIG[@]}" ../..