获取由命令 add_definitions() 和 add_compile_options() 设置的 C/CXX FLAGS

Get C/CXX FLAGS set by commands add_definitions() and add_compile_options()

在我的 CMakeLists.txt 中,全局 C 和 CXX 标志是使用命令设置的 add_definitions()add_compile_options.

多次尝试检索 C/CXX 标志:

  1. ${CMAKE_C_FLAGS}${CMAKE_CXX_FLAGS} 为空
  2. ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}是原来默认的CMake值
    (例如 -g 对应 CMAKE_C_FLAGS_DEBUG
  3. ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}同上
  4. get_cmake_property(def COMPILE_DEFINITIONS)NOTFOUND
  5. get_cmake_property(opt COMPILE_OPTIONS) 也是 NOTFOUND

我想将自定义 C/CXX 标志传递给基于 ./configure:

的外部项目
ExternalProject_Add( my_external_lib
  DEPENDS            Threads::Threads
  SOURCE_DIR         ${CMAKE_CURRENT_LIST_DIR}
  BUILD_IN_SOURCE    1
  UPDATE_COMMAND     echo "Full clean" && make distclean
  CONFIGURE_COMMAND  ${CMAKE_CURRENT_LIST_DIR}/configure
       --prefix=${CMAKE_BINARY_DIR}
       --cc=${CMAKE_C_COMPILER}
       --cxx=${CMAKE_CXX_COMPILER}
       --CFLAGS=${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}
       --CXXFLAGS=${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}
       --LDFLAGS=${CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE}}
       --ARFLAGS=${CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE}}
       --enable-static
)

(1) 如何获取add_definitions()add_compile_options设置的编译标志?

注意:我使用的CMake版本是3.3.2


在写问题的时候,我意识到我在想另一个问题:

我使用 ccache 但是 ./configure 脚本看不到我的环境变量 CCCXX 当我这样设置它们时:

get_cmake_property(launcher   RULE_LAUNCH_COMPILE)
set(ENV{CC}  ${launcher} ${CMAKE_C_COMPILER})
set(ENV{CXX} ${launcher} ${CMAKE_CXX_COMPILER})

(2) 以上CMake语句是否正确?


哎呀我又问了一个问题:

我从同一个变量 ${CMAKE_STATIC_LINKER_FLAGS} 中检索 LDFLAGSARFLAGS
(3) 是否还有CMake变量可以设置ARFLAGS?

一题多题post,但都很简单:

  1. add_definitions() 的文档 link 明确引用了 directory and target COMPILE_DEFINITION properties. The first of them is extracted by using get_directory_property(), the second by using get_target_property(). Any of them can also be extracted by using get_property()get_cmake_property 您尝试使用的在这里不适用(它用于其他类型的属性)。

  2. 您在 configuration 步骤设置了环境变量 CC 和 CXX,但是 ExternalProject_Add 的所有命令都在 build步.

目前ExternalProject_Add不支持简单设置环境变量。根据 this bugreport 中的建议,对于不带空格的设置变量值 ,您可以在命令前加上 "VAR=VAL" 子句。但是对于您的情况,由于转义问题,这将不起作用。

另请参阅 CMake 常见问题解答:How can I get or set environment variables?

[...]
environment variables SET in the CMakeLists.txt only take effect for cmake itself (configure-time), so you cannot use this method to set an environment variable that a custom command might need (build-time). Barring environment variable support by various CMake commands (e.g. add_custom_command(), currently not supported yet), an acceptable workaround may be to invoke shell scripts instead which wrap the commands to be executed.

用于设置环境变量和运行实际命令的简单wrapper脚本:

wrapper.sh.in:

export "CC=ccache @CMAKE_C_COMPILER@"
export "CXX=ccache @CMAKE_XX_COMPILER@"
eval "$*"

CMakeLists.txt:

configure_file(wrapper.sh.in wrapper.sh @ONLY)
ExternalProject_Add(my_external_lib
    ...
    CONFIGURE_COMMAND /bin/sh ${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh
        ${CMAKE_CURRENT_LIST_DIR}/configure ...
    BUILD_COMMAND /bin/sh ${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh make
)

请注意,您应该为每个需要此变量的命令使用包装器。在你的例子中,这些是 CONFIGURE_COMMAND 和 BUILD_COMMAND.

  1. Documentation for CMAKE_STATIC_LINKER_FLAGS says that these flags are used for linker, so you may probably use them as LDFLAGS. As for ARFLAGS, they are hardcoded directrly to CMAKE_C_ARCHIVE_CREATE 变量,包含 ar 的完整命令行。默认情况下,此命令仅使用 linker 标志,因此您的方法似乎也是正确的。