Compiler flags from CMakeLists.txt do not display in CMake-Gui or CMakeCache.txt

I was just starting to learn CMake and thought that I would understand the basic process of first writing CMakeLists.txt , and then the settings for creating CMakeCache.txt and finally generating Makefiles .

However, when I try to apply it to the next CMakeLists.txt, I do not get the expected results, and I'm not sure what is going wrong. The CMakeLists.txt part looks like this:

 # compiler flags if (CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive -Wall -Wformat-security") if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-local-typedefs") endif() endif() 

Since I am using gcc / g ++ 4.7.3, the compiler flags from the first if-statement should be set. But if I configure this with CMake-Gui, the compiler flags will not be predefined. The same thing happens when I do not comment on if-statements and just save set(CMAKE_CXX_FLAGS ...) . When searching for CMakeCache.txt for any -std=c++11 flags, I also don't get any results.

Why is this happening? What is the point of indicating compiler flags inside CMakeLists.txt when not in use? Or I get something completely wrong and they are used, but then I don’t know why and how I could check.

When I create an actual project (Eclipse CDT) with make and import it into Eclipse, I get error messages that cannot be resolved with C ++ 11, the __cplusplus macro contains the value 199711 so the -std=c++11 flag is obviously not used.

+7
c ++ gcc c ++ 11 cmake
source share
2 answers

The flags you specify in the CMakeLists.txt file are probably used correctly by the compiler. You cannot see them directly in CMakeCache.txt, but:

  • You can see the command lines by running make VERBOSE=1 instead of the standard make
  • In addition, you can set CMAKE_VERBOSE_MAKEFILE to 1 to enable printing of commands (this can be found by checking the "Advanced" checkbox in the CMake GUI)
  • As @Angew said, if you really want to see updated flags in the CMake GUI, set your variables using CACHE FORCE

As an example, I have been using this configuration in my project for some months and have never had a problem:

 if(MSVC) # MSVC compiler (Win32 only) # Display more warnings set(CMAKE_CXX_FLAGS "/W3") elseif(UNIX OR CMAKE_COMPILER_IS_GNUCXX) # Clang OR Gcc (Linux, Mac OS or Win32 with MingW) # Enable C++11 and displays all warnings set(CMAKE_CXX_FLAGS "-Wall -std=c++11") if(APPLE) # Clang / Mac OS only # Required on OSX to compile c++11 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -mmacosx-version-min=10.7") endif(APPLE) endif() 

Update

Starting with CMake 3.0, you can replace set(CMAKE_CXX_FLAGS "...") with add_compile_options(-std=c++11)

CMake 3.1 introduced a new syntax for configuring a compiler with a specific version in C ++:

 set(CMAKE_CXX_STANDARD 11) 
+6
source share

At first, you can set a variable to a value only if it is no longer in the cache. The last parameter is a description that we do not need, since we will redefine it anyway.

 set(VARIABLE "Hello World!" CACHE STRING "") 

Then force the value into the cache using the existing value from the line above. Since this is cached, users can still change the variable, and it will not be discarded every time.

 set(VARIABLE ${VARIABLE} CACHE STRING "Description." FORCE) 

This is a bit hacked in CMake, as you can see, but it works reliably.

+4
source share

All Articles