0

I need to check which options and flags are passed to compiler during build. Printing out environment variable doesn't show anything. It's just empty.

CMakeLists.txt contains:

cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
project(GtestExample LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

message(STATUS "compiler ${CMAKE_CXX_COMPILER}")

#adding subdirecotories, linking libs etc
# ...

add_compile_options("-O0")
add_compile_options("-Wall")
message(STATUS "C++ compiler flags: ${CMAKE_CXX_FLAGS}")

Windows terminal output:

C:\Users\NDanashevskii\Documents\cplusplus\cplusplus_test\cmake_out>cmake ..
-- compiler C:/MinGW/bin/g++.exe
-- C++ compiler flags:
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/NDanashevskii/Documents/cplusplus/cplusplus_test/cmake_out

Could you please support in solving this issue?

r_spb
  • 75
  • 1
  • 14
  • The command `add_compile_options` doesn't modify `CMAKE_CXX_FLAGS` variable. Instead, it modifies `COMPILE_OPTIONS` directory property. It is clearly stated in the [documentation](https://cmake.org/cmake/help/v3.15/command/add_compile_options.html). And `CMAKE_CXX_FLAGS` is not an *environment* variable. It is CMake variable. – Tsyvarev Jan 14 '20 at 10:15
  • @Tsyvarev Correct, thank you for your answer. But COMPILE_OPTIONS variable is empty as well even after `add_compile_option()` usage. Is there any way to check all compiler flags in summary? – r_spb Jan 14 '20 at 12:52
  • A *variable* and a *property* are different things in CMake. "Is there any way to check all compiler flags in summary?" - When build the project with `make` (after `cmake`), you may pass additional option `VERBOSE=1`. So every executed command line will be printed, and you may check that compiling your executable or library uses required compiler flags. – Tsyvarev Jan 14 '20 at 13:15
  • @Tsyvarev Executing `cmake --build ./ -- VERBOSE=1` shows me inputs for `cmake.exe` and `mingw32-make.exe` but not for `g++.exe`. Could you please tell how can I check g++ inputs? – r_spb Jan 15 '20 at 09:36
  • Hm, this should work: `mingw32-make VERBOSE=1`. Note that only those command lines are printed which are actually executed. That is if your executable is already built and doesn't need to be rebuilt, nothing will be printed. You may run `mingw32-make clean` for remove already build files. – Tsyvarev Jan 15 '20 at 09:51
  • @Tsyvarev Thanks a lot! `set(CMAKE_VERBOSE_MAKEFILE ON)` or `cmake .. -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON` show full make output during clean build as well. – r_spb Jan 15 '20 at 10:50

1 Answers1

0

To check compiler inputs full make utility execution should be switched on during cmake --build. There are few ways:

  1. cmake .. then `mingw32-make VERBOSE=1'
  2. cmake .. -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON then cmake --build . -- VERBOSE=1
  3. Add set(CMAKE_VERBOSE_MAKEFILE ON) to CMakeLists.txt and cmake --build . -- VERBOSE=1

And execute build after clean to check full output.

r_spb
  • 75
  • 1
  • 14