如何打印当前使用target_compile_options()设置的编译标志?

14
我试图打印为目标设置的编译标志。最好的情况是在配置和编译时打印当前标志的一行,但如果不可能,则仅在配置时间(或编译时间)打印(可接受的解决方案)。
这是我的测试.c文件:
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

和 CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(cmake_gcc_options_try_c C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

add_executable(cmake_gcc_options_try_c main.c)

target_compile_options(cmake_gcc_options_try_c 
                       PUBLIC -W -Wall -Wextra -pedantic -pedantic-errors)

# This fails
message("-- Current compiler flags CMAKE_C_FLAGS are: ${CMAKE_C_FLAGS}")
message("-- Current compiler flags C_FLAGS are: ${C_FLAGS}")

cmake . && make

给出以下输出:

-- The C compiler identification is GNU 7.4.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
-- Current compiler flags CMAKE_C_FLAGS are: 
-- Current compiler flags C_FLAGS are: 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Projects/cmake-gcc-options-try-c
Scanning dependencies of target cmake_gcc_options_try_c
[ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
[100%] Linking C executable cmake_gcc_options_try_c
[100%] Built target cmake_gcc_options_try_c

为什么会打印出未定义的CMAKE_C_FLAGSC_FLAGS

如何在make命令中实现这种打印效果:

[ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
[ 50%] Current compiler flags are: -W -Wall -Wextra -pedantic -pedantic-errors -std=gnu11
[100%] Linking C executable cmake_gcc_options_try_c
[100%] Built target cmake_gcc_options_try_c

更新:维克托·谢尔盖恩科提供了一个可行的解决方案,但有一个问题是它没有那么美观的格式: 输入图像说明 有什么想法可以使它按照其他打印格式呈现?例如:

[ 50%] Building C object CMakeFiles/cmake_gcc_options_try_c.dir/main.c.o
[100%] Linking C executable cmake_gcc_options_try_c
[100%] Current compiler flags are: -W -Wall -Wextra -pedantic -pedantic-errors -std=gnu11
[100%] Built target cmake_gcc_options_try_c

第二个问题是并没有打印出-std=gnu11(但是已经通过set(CMAKE_C_STANDARD 11)set(CMAKE_C_STANDARD_REQUIRED ON)启用了它)


1
为什么 CMAKE_C_FLAGSC_FLAGS 被打印出来却未定义?变量CMAKE_C_FLAGS在你的情况下是空的,它不会积累使用target_compile_options添加的值。后一选项以目标的属性形式出现。有关更多信息,请参见该问题:https://dev59.com/lFkS5IYBdhLWcg3wvI20。如果您想提取目标的编译定义(例如打印它们),则需要提取相应的属性,就像那个答案中所示:https://dev59.com/K4Hba4cB1Zd3GeqPVL_N#24921921。 - Tsyvarev
1个回答

10

用法:

这为我提供了一个在配置和编译时都可以获取选项列表的;-list

cmake_minimum_required(VERSION 3.10)
project(cmake_gcc_options_try_c C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

add_executable(cmake_gcc_options_try_c main.c)

target_compile_options(cmake_gcc_options_try_c 
                       PUBLIC -W -Wall -Wextra -pedantic -pedantic-errors)

get_target_property(MAIN_CFLAGS cmake_gcc_options_try_c COMPILE_OPTIONS)
# also see: COMPILE_DEFINITIONS INCLUDE_DIRECTORIES
message("-- Target compiler flags are: ${MAIN_CFLAGS}")

add_custom_command(TARGET cmake_gcc_options_try_c POST_BUILD
COMMAND echo built with the flags: ${MAIN_CFLAGS})

问题更新后的更新:要获取C/CXX标准,请查阅C_STANDARD。如果您想知道为什么CMake设置了标志的-gnu变体,那是因为CXX_EXTENSIONS默认情况下为ON

更新2:要将每个源文件的完整编译器/链接器命令作为JSON文件获取,请将CMAKE_EXPORT_COMPILE_COMMANDS设置为ON(仅适用于CMake 3.5+、makeninja生成器)。这一点归功于Florian在这个问题中的评论。


更新1)没有使用target_compile_options设置,更新2)是事后添加的不同问题;恐怕无法在编译时访问CMake编译进度或通过CMake打印消息。 - Victor Sergienko
是的,你说得对,我没有提到我想要打印所有标志,但如果你看一下历史记录,在第一个版本中有一行 [50%]当前编译器标志为:-W -Wall -Wextra -pedantic -pedantic-errors -std = gnu11(我隐含地意味着它)。否则,打印部分标志有什么意义? :) 顺便说一句,cmake会创建一个名为 flags.make 的文件,里面包含了所有标志。 - stackoverflower
1
如果你想要所有编译标志,那么你的问题就是一个重复的问题,参考 https://dev59.com/m1oU5IYBdhLWcg3wRFgh。打印格式或缺乏格式完全不在问题的范围内,特别是你已经将其标记为规范。`flags.make` 是一个实现细节,在 ninja 中是不同的,并且在其他生成器中不存在。 - Victor Sergienko
“CMake仅设置-gnu标志变量”:这并不正确,您可以使用CXX_EXTENSIONS来使用非-gnu变体。 - Tachi
谢谢!这就是为什么它总是被设置的。正在更新答案。 - Victor Sergienko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接