gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:14:32 25 4
gpt4 key购买 nike

我正在尝试打印为目标设置的编译标志。最好的情况是在配置和编译时打印一 strip 有当前标志的行,但是,如果不可能,则仅在配置时(或仅在编译时)(可接受的解决方案)。

这是我的测试.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



更新: Viktor Sergienko带有一个可行的解决方案,但是与此相关的一个问题是它的打印效果不是很好:
enter image description here
有什么想法要使其成为其他打印品的格式吗?例如:
[ 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获取目标特定的编译标志;
  • 带有POST_BUILD选项的
  • add_custom_command可以打印任何变量;

  • 这给了我在配置和编译时的选项的 ;-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
    Update2:要将每个源文件的完整编译器/链接器命令作为JSON文件获取,请将 CMAKE_EXPORT_COMPILE_COMMANDS设置为 ON(仅在CMake 3.5 +, makeninja生成器中有效)。值得赞扬的是Florian在 this question中的评论。

    关于makefile - 如何打印使用target_compile_options()设置的当前编译标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56104607/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com