CMake Cookbook
上QQ阅读APP看书,第一时间看更新

There is more

Where can we find which default compilers and compiler flags will be picked up by CMake for our platform? CMake offers the --system-information flag, which will dump all information about your system to the screen or a file. To see this, try the following:

$ cmake --system-information information.txt

Searching through the file (in this case, information.txt), you will find the default values for the CMAKE_CXX_COMPILER, CMAKE_C_COMPILER, and CMAKE_Fortran_COMPILER options, together with their default flags. We will have a look at the flags in the next recipe.

CMake provides additional variables to interact with compilers:

  • CMAKE_<LANG>_COMPILER_LOADED: This is set to TRUE if the language, <LANG>, was enabled for the project.
  • CMAKE_<LANG>_COMPILER_IDThe compiler identification string, unique to the compiler vendor. This is, for example, GCC for the GNU Compiler Collection, AppleClang for Clang on macOS, and MSVC for Microsoft Visual Studio Compiler. Note, however, that this variable is not guaranteed to be defined for all compilers or languages.
  • CMAKE_COMPILER_IS_GNU<LANG>: This logical variable is set to TRUE if the compiler for the language <LANG> is part of the GNU Compiler Collection. Notice that the <LANG> portion of the variable name follows the GNU convention: it will be CC for the C language, CXX for the C++ language, and G77 for the Fortran language.
  • CMAKE_<LANG>_COMPILER_VERSION: This variable holds a string with the version of the compiler for the given language. The version information is given in the major[.minor[.patch[.tweak]]] format. However, as for CMAKE_<LANG>_COMPILER_ID, this variable is not guaranteed to be defined for all compilers or languages.

We can try to configure the following example CMakeLists.txt with different compilers. In this example, we will use CMake variables to probe what compiler we are using and what version:

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-06 LANGUAGES C CXX)

message(STATUS "Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}")
if(CMAKE_CXX_COMPILER_LOADED)
message(STATUS "The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCXX}")
message(STATUS "The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}")
endif()

message(STATUS "Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}")
if(CMAKE_C_COMPILER_LOADED)
message(STATUS "The C compiler ID is: ${CMAKE_C_COMPILER_ID}")
message(STATUS "Is the C from GNU? ${CMAKE_COMPILER_IS_GNUCC}")
message(STATUS "The C compiler version is: ${CMAKE_C_COMPILER_VERSION}")
endif()

Observe that this example does not contain any targets, so there is nothing to build and we will only focus on the configuration step:

$ mkdir -p build
$ cd build
$ cmake ..

...
-- Is the C++ compiler loaded? 1
-- The C++ compiler ID is: GNU
-- Is the C++ from GNU? 1
-- The C++ compiler version is: 8.1.0
-- Is the C compiler loaded? 1
-- The C compiler ID is: GNU
-- Is the C from GNU? 1
-- The C compiler version is: 8.1.0
...

The output will of course depend on the available and chosen compilers and compiler versions.