Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Flags from CMakeLists.txt don't appear in CMake-Gui or CMakeCache.txt

Tags:

c++

gcc

c++11

cmake

I just started to learn CMake and thought I would have understood the basic process of first writing the CMakeLists.txt, then configuring to generate the CMakeCache.txtand at the end generating the Makefiles.

However, when I try to apply it to the following CMakeLists.txt, I'm not getting the expected results and I'm not sure what is going wrong. Part of the CMakeLists.txt 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'm 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, there are no compiler flags pre-defined whatsoever. The same happens when I out-comment the if-statements and just keep the set(CMAKE_CXX_FLAGS ...). When searching the CMakeCache.txt for any -std=c++11 flags, I don't get any results, too.

Why does this happen? What's the point of specifying compiler flags inside the CMakeLists.txt when they aren't used? Or am I getting something completely wrong and they are used, but then I don't know why and how I could check.

When generating the actual (Eclipse CDT) project with make and importing it to Eclipse, I'm getting error messages that C++11 features can't be resolved, the __cplusplus macro contains the value 199711 so the -std=c++11 flag is obviously not used.

like image 713
Schnigges Avatar asked Jan 03 '14 11:01

Schnigges


People also ask

Where is CMakeCache TXT located?

The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.

Where can I find CMakeLists txt?

The CMakeLists. txt file is at the top directory of the ROOT git repository. If you don't have that file, then there is a problem with your git checkout. Please make sure that the path you provide to CMake is the path to the top directory of the repository, and that the file is there.

How do I add Cflags to CMake?

You need to set the flags after the project command in your CMakeLists. txt. Also, if you're calling include(${QT_USE_FILE}) or add_definitions(${QT_DEFINITIONS}) , you should include these set commands after the Qt ones since these would append further flags.


1 Answers

You can first, set the variable to a value only if it is not in cache already. The last parameter is the description which we don't need since we'll override it anyway.

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

Then force the value into cache using its existing value from the line above. Since that is cached, users can still change the variable and it won't be set back every time.

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

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

like image 129
danijar Avatar answered Sep 22 '22 09:09

danijar