web-dev-qa-db-ja.com

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 は有効な解決策が付属していましたが、これに関する1つの問題はそれほどきれいに印刷されていません: 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
_

2番目の問題は、_-std=gnu11_が出力されないことです(ただし、set(CMAKE_C_STANDARD 11)およびset(CMAKE_C_STANDARD_REQUIRED ON)で有効になります)。

9
stackoverflower

使用する:

これにより、構成時とコンパイル時の両方でオプションの;-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バリアントのみを設定します。

Update2:すべてのソースファイルの完全なコンパイラ/リンカーコマンドをJSONファイルとして取得するには、 CMAKE_EXPORT_COMPILE_COMMANDSONに設定します(CMake 3.5以降、makeでのみ機能し、 ninjaジェネレーター)。この作品のクレジットは、フロリアンのコメント この質問 にあります。

6