如何打印使用 [=10=]() 设置的当前编译标志?
How to print current compilation flags that are set with target_compile_options()?
我正在尝试打印为目标设置的编译标志。最好的方案是在配置和编译时间打印一行带有当前标志的行,但如果不可能,则仅在配置时间(或仅编译)(可接受的解决方案)。
这是我的测试 .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
?
更新: Viktor Sergienko 提供了一个可行的解决方案,但有一个问题,它的打印效果不是很好:
任何想法如何使它成为其他印刷品的格式?例如:
[ 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)
启用)
使用:
- COMPILE_OPTIONS target property 获取 target-specific 编译标志;
- add_custom_command 带有 POST_BUILD 选项来打印任何变量;
这为我提供了 ;-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. If you wonder why CMake sets -gnu
variant of the flag, it's because of CXX_EXTENSIONS
默认为ON
。
Update2:要将每个源文件的完整 compiler/linker 命令作为 JSON 文件,请设置 CMAKE_EXPORT_COMPILE_COMMANDS to ON
(only works in CMake 3.5+, make
and ninja
generators). Credit for this piece goes to Florian's comment in this question.
我正在尝试打印为目标设置的编译标志。最好的方案是在配置和编译时间打印一行带有当前标志的行,但如果不可能,则仅在配置时间(或仅编译)(可接受的解决方案)。
这是我的测试 .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
?
更新: Viktor Sergienko 提供了一个可行的解决方案,但有一个问题,它的打印效果不是很好:
[ 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)
启用)
使用:
- COMPILE_OPTIONS target property 获取 target-specific 编译标志;
- add_custom_command 带有 POST_BUILD 选项来打印任何变量;
这为我提供了 ;-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. If you wonder why CMake sets -gnu
variant of the flag, it's because of CXX_EXTENSIONS
默认为ON
。
Update2:要将每个源文件的完整 compiler/linker 命令作为 JSON 文件,请设置 CMAKE_EXPORT_COMPILE_COMMANDS to ON
(only works in CMake 3.5+, make
and ninja
generators). Credit for this piece goes to Florian's comment in this question.