如何通过 CLI 将带有空格的变量传递给 cmake?

发布于 2025-01-10 12:00:32 字数 1223 浏览 0 评论 0原文

第三方 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2025-01-17 12:00:32

您需要引用整个 -D 参数,例如:

$ cmake ../ '-DCMAKE_CXX_FLAGS=-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer'
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/baiyanh/arena/cmake/tut/build

然后编译如下所示:

$ cmake --build . -v
...
[ 50%] Building CXX object CMakeFiles/tut.dir/tut.cpp.o
/usr/bin/c++    -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer   -o CMakeFiles/tut.dir/tut.cpp.o -c /home/baiyanh/arena/cmake/tut/tut.cpp
...

You need to quote the whole -D parameter, like:

$ cmake ../ '-DCMAKE_CXX_FLAGS=-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer'
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/baiyanh/arena/cmake/tut/build

Then the compile looks like:

$ cmake --build . -v
...
[ 50%] Building CXX object CMakeFiles/tut.dir/tut.cpp.o
/usr/bin/c++    -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer   -o CMakeFiles/tut.dir/tut.cpp.o -c /home/baiyanh/arena/cmake/tut/tut.cpp
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文