3

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
  • 19,451
  • 13
  • 99
  • 197
El Marce
  • 3,144
  • 1
  • 26
  • 40

1 Answers1

4

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.
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Hi Antonio, big thanks for the answer, I didn’t put in my question the actual flags I'm using. I'll check the VERBOSE option and I'll tell you – El Marce Apr 19 '15 at 21:45
  • Hi @Antonio, I set verbose as you said, my build is failing because the -std=gnu99 flag is not set in the compiler. However I'm setting this flag as you suggested in your example in my cmake file. Still no work :(. Any ideas? Thanks – El Marce Apr 20 '15 at 18:47
  • @ElMarce Which kind of files are you building? C or C++? – Antonio Apr 20 '15 at 18:49
  • When I put the option in the GUI (therefore in the cache value I guess) the build succeed – El Marce Apr 20 '15 at 19:03
  • @ElMarce That's weird. Which cmake version are you using? – Antonio Apr 20 '15 at 19:04
  • Cmake: 2.8.12.2. I'm in linux ubuntu 14.0.4 – El Marce Apr 20 '15 at 19:05
  • @ElMarce I have posted a relevant part of the documentation, I confirm there must be something to fix in your CMakeLists.txt – Antonio Apr 20 '15 at 19:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75780/discussion-between-el-marce-and-antonio). – El Marce Apr 21 '15 at 08:20