我试图打印为目标设置的编译标志。最好的情况是在配置和编译时打印当前标志的一行,但如果不可能,则仅在配置时间(或编译时间)打印(可接受的解决方案)。
这是我的测试.c文件:
这是我的测试.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_FLAGS
和C_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)
启用了它)
CMAKE_C_FLAGS
和C_FLAGS
被打印出来却未定义?变量CMAKE_C_FLAGS
在你的情况下是空的,它不会积累使用target_compile_options
添加的值。后一选项以目标的属性形式出现。有关更多信息,请参见该问题:https://dev59.com/lFkS5IYBdhLWcg3wvI20。如果您想提取目标的编译定义(例如打印它们),则需要提取相应的属性,就像那个答案中所示:https://dev59.com/K4Hba4cB1Zd3GeqPVL_N#24921921。 - Tsyvarev