gpt4 book ai didi

bash - 使用 bash 变量参数调用 cmake

转载 作者:行者123 更新时间:2023-12-05 03:15:07 25 4
gpt4 key购买 nike

我正在努力解决一个奇怪的问题。我正在尝试使用 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 变量,但未能成功实现我的目标。

有什么提示吗?

最佳答案

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

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

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

关于bash - 使用 bash 变量参数调用 cmake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20570042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com