将命令行实用程序的输出传递给编译器/链接器 [英] Pass output of command line utility to compiler/linker
问题描述
我想将 getconf PAGESIZE
命令输出的结果作为预处理器定义传递给我的程序,格式为 -DPAGESIZE =`getconf PAGESIZE`
在自定义分配器声明中用于 [[gnu :: assume_aligned(PAGESIZE)]
)。
I want to pass result of the getconf PAGESIZE
command output as preprocessor define to my program in form of -DPAGESIZE=`getconf PAGESIZE`
for [[gnu::assume_aligned(PAGESIZE)]]
in custom allocator declaration.
我尝试了以下操作:
add_definitions(-DPAGESIZE=`getconf PAGESIZE`)
但它的扩展范围完全与 -DPAGESIZE = \`getconf ... PAGESIZE`
,其中 ...
是其他 CMAKE_CXX_FLAGS *
的内容。即在 CMakeLists.txt
文件中存在转义符转义的问题。
But it expanded exactly as -DPAGESIZE="\`getconf" ... PAGESIZE`
, where ...
is contents of other CMAKE_CXX_FLAGS*
. I.e. there is an issue with escaping of backticks in CMakeLists.txt
files.
如何正确地将这样的参数传递给 CMakeLists.txt
文件中的编译器/链接器?也许还有另一种方法可以达到期望?
How to properly pass such an arguments to compiler/linker in CMakeLists.txt
files? Maybe is there another way to achieve desired?
我还尝试了 add_definitions(-DPAGESIZE = $$(getconf PAGESIZE))
( $$
由 cmake
$ >),但 -DPAGESIZE
和其余部分由 cmake
分割。 add_definitions(-DPAGESIZE = $$(getconf PAGESIZE))
使 cmake
避开每个美元符号。 / p>
Also I tried add_definitions(-DPAGESIZE="$$(getconf PAGESIZE)")
($$
expanded as $
by cmake
), but -DPAGESIZE
and the rest part are splitted by cmake
. add_definitions("-DPAGESIZE=$$(getconf PAGESIZE)")
makes cmake
to escape every dollar sign though.
推荐答案
根据 add_definitions 命令,传递给此命令的预处理器定义将附加到 COMPILE_DEFINITIONS
属性:
According to documentation for add_definitions command, preprocessor definitions, passed to this command, are appended to COMPILE_DEFINITIONS
property:
以-D或/ D开头的看起来像预处理程序定义的标志会自动添加到当前目录的COMPILE_DEFINITIONS目录属性中。
Flags beginning in -D or /D that look like preprocessor definitions are automatically added to the COMPILE_DEFINITIONS directory property for the current directory.
和 COMPILE_DEFINITIONS
属性的内容,根据其文档始终由CMake进行转义,因此您不能在构建命令中保留反引号的特殊含义:
And content of COMPILE_DEFINITIONS
property, according to its documentation is always escaped by CMake, so you cannot preserve special meaning of backticks in the build command:
CMake wi ll将自动为本机构建系统正确地忽略该值
CMake will automatically escape the value correctly for the native build system
您可以手动修改CMAKE_CXX_FLAGS,如您的注释中所示。
Your may modify CMAKE_CXX_FLAGS manually, as you show in your comment.
更好的方法是在 configuration 阶段使用 execute_process
命令运行所需的命令,并使用它的输出用于 add_definitions
命令。 (或使用此输出通过 configure_file
创建其他头文件)。
The better way is to use execute_process
command for run needed command at configuration stage, and use its output for add_definitions
command. (Or use this output for create additional header file with configure_file
).
这篇关于将命令行实用程序的输出传递给编译器/链接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!