CMake SET command not working as expected (CMake not setting value of predefined cached variable)

El Marce

I'm using CMake in my builds. I'm trying to set compiler flags like this:

set(CMAKE_C_FLAGS_DEBUG "-option1 -option2 -option3")

however, my build fails because one of the compiler flags is not set.

When I check the value of the variable in CMake-GUI is empty:

enter image description here

Can someone point out what is happening?

Antonio

What you see with cmake-gui is the cache status. You only see there variables that you explicitly cache, or predefined cmake cached variables.

Now, when you do:

set(CMAKE_C_FLAGS_DEBUG "-option1 -option2 -option3")

You are doing something particular: you are setting a "local" (not cached) variable which has the same name of a predefined cmake cached variable. In C++ it would be like defining a local variable with the same name of a global variable: the local variable will hide your global variable.

From the set documentation. (The documentation calls "normal" what I called "local")

Both types can exist at the same time with the same name but different values. When ${FOO} is evaluated, CMake first looks for a normal variable 'FOO' in scope and uses it if set. If and only if no normal variable exists then it falls back to the cache variable 'FOO'.

You are already effectively setting CMAKE_C_FLAGS_DEBUG, and the compiler will use the flags you have specified. You can check it with:

message(STATUS "CMAKE_C_FLAGS_DEBUG = ${CMAKE_C_FLAGS_DEBUG}")

So, your building is failing for another reason. Check which command line make generates: make VERBOSE=1.

By the way, I suggest you append your flags to the predefined ones, by doing:

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -option1 -option2 -option3")

Also consider using the other predefined variable CMAKE_C_FLAGS, in case you want those settings to be propagated to all build types.


Edit:
After some iterations it turned out the problem was that the build type (CMAKE_BUILD_TYPE) was not set from cmake. This can either be done through the cmake-gui interface, or adding something like -DCMAKE_BUILD_TYPE=Debug or -DCMAKE_BUILD_TYPE=Release to the cmake command line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CMake SET command not working as expected (CMake not setting value of predefined cached variable)

From Dev

Confused about CMake's cached variable setting priority

From Dev

Ambiguous documentation (until Ver 3.2) for how to define a (cached) variable in cmake from command line?

From Dev

Using cmake variable in execute_process command in cmake file

From Dev

Command: `cmake . .`

From Dev

cmake add_custom_command not working

From Dev

CMake command not working in VS Code task

From Java

How to set a CMake option() at command line

From Dev

How to set a CMake path from command line

From Dev

How to set a CMake path from command line

From Dev

Make cmake pass command line variable to compiler

From Dev

How to set SHELL variable in makefiles generated by CMake?

From Dev

CMake: Set variable from nested function calls

From Dev

Change flag value in CMake if it is already set

From Dev

Set Variable to value of command

From Dev

source_group CMake command isn't working

From Dev

source_group CMake command isn't working

From Dev

Rule to set a variable in Makefile not working as expected

From Dev

How to set runtime PATH for CMake custom command on Windows

From Dev

Cmake: add_custom_command argument based on variable content

From Dev

CMake command is not recognized

From Dev

CMake command for paths are equal

From Dev

cmake command line option

From Dev

Can not generate a CMake command

From Dev

Qt w/ Cmake: set(QT_USE_QTWEBKIT TRUE) not working

From Dev

CMake integer comparison not working

From Dev

CMake integer comparison not working

From Dev

Error "command not found" when setting value to variable

From Dev

How do I check if an environment variable is set in cmake

Related Related

HotTag

Archive