如何通过 CLI 将带有空格的变量传递给 cmake?
第三方 CMake 文件中定义了一个选项。
SET(PHYSX_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -ffunction-sections -fdata-sections -fno-strict-aliasing ${GCC_WARNINGS}" CACHE INTERNAL "PhysX CXX")
我正在尝试将自定义标志传递给 CMAKE_CXX_FLAGS:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS="-Wno-restrict -Wno-class-memaccess"
因为两个 GCC 警告标志之间有空格,所以最终结果最终会破坏字符串:
CXX_FLAGS = "-Wno-restrict ... # Quote is not closed
,我会收到有关未终止字符串的错误
当我不使用引号时 围绕标志 CMake 变量值:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS=-Wno-restrict -Wno-class-memaccess
最终结果忽略空格后面的值,这是有道理的,因为空格将被视为单独的变量。
我可以做些什么来将带空格的值设置到 CMake 变量中吗?
谢谢@KamilCuk,
我的问题是触发命令的Python 脚本。我在 Python 中有以下行:
subprocess.run(parsedCmdLine.split(' '), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
幸运的是,Python 有一个类似于 shell 的词法实用程序模块:
import shlex
subprocess.run(shlex.split(parsedCmdLine.split), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
There is an option defined in a third party CMake file.
SET(PHYSX_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -ffunction-sections -fdata-sections -fno-strict-aliasing ${GCC_WARNINGS}" CACHE INTERNAL "PhysX CXX")
I am trying to pass custom flags to CMAKE_CXX_FLAGS
:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS="-Wno-restrict -Wno-class-memaccess"
Because I have space between the two GCC warning flags, the final result ends up breaking the string:
CXX_FLAGS = "-Wno-restrict ... # Quote is not closed
and I get an error about unterminated string
WHen I use no quotes around the flag CMake variable value:
cmake physx/sources/compiler/cmake -B build -DCMAKE_CXX_FLAGS=-Wno-restrict -Wno-class-memaccess
The final result ignores the value after space, which makes sense since space will be treated as a separate variable.
Is there something that I can do to set values with space into CMake variable?
Thanks @KamilCuk,
My problem was with the Python script that was triggering the commands. I had the following line in Python:
subprocess.run(parsedCmdLine.split(' '), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
Luckily, Python has a lexical utility module that resembles shell:
import shlex
subprocess.run(shlex.split(parsedCmdLine.split), shell=platform.system() == 'Windows', cwd=x['sourceDir'])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要引用整个 -D 参数,例如:
然后编译如下所示:
You need to quote the whole -D parameter, like:
Then the compile looks like: