3

I am wondering if it is possible to append to inferred variables when calling cmake. Due either the compiler or the app supplied CMakeLists.txt C++11 support isn't detected by cmake even though it is supported and the application requires it. If I do:

cmake-DCMAKE_CXX_FLAGS='-std=c++11' ../source_dir

The flags get overwritten and I wouldn't like to lose the inferred flags. I could manually run without overwriting and just copy the flags and append c++11 but I think there must be a better solution.

I have found a number of posts on adding c++11 support to CMakeLists.txt but not in cmake call, so I am wondering if this is possible at all. Please let me know.

3 Answers3

3

Yes, this is possible. First, create a variable called MY_FLAGS in the command line with the flags you want.

cmake -DMY_FLAGS='-std=c++11' ../source_dir

Then, in your CMakeLists.txt, do the following:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}")

This will effectively amend your flags to the flags that CMake generates. However, if you want to set the C++ standard to C++11, you can just do the following without directly dealing with flags:

cmake -DCMAKE_CXX_STANDARD=11 -DCMAKE_CXX_STANDARD_REQUIRED=ON ../source_dir

On a side note, make sure that the ../source_dir is after the CMake options, since otherwise CMAKE_CXX_STANDARD_REQUIRED would not work. (Read)

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
2

I recently did this, when building a dependency for one of the applications I work to. There are 2 CMake variables that you can make use of, namely:

-DCMAKE_BUILD_TYPE=Debug/Release
-DCMAKE_CXX_FLAGS_[DEBUG/RELEASE]

When specifying a build type, cmake will add to the compiler flags the flags that are specified in that second variable. So for me, using:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS_RELEASE=-fPIC

did the thing.

In your case, you can try the following approaches:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE=-std=c++11
or
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS_DEBUG=-std=c++11

Of course, this will work if the variables are not overwritten in the script. Try both approaches.

Sebi
  • 428
  • 5
  • 11
0

You are probably looking for

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

Or via command line:

cmake -DCMAKE_CXX_STANDARD=11 -DCMAKE_CXX_STANDARD_REQUIRED=ON

That being said, CMAKE_CXX_FLAGS is not overridden by cmake, so your CMakeLists.txt must be doing it somewhere. The polite way of setting the flags from within a script is to append to the variable, that is:

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} stuff")
sbabbi
  • 11,070
  • 2
  • 29
  • 57
  • 1
    So I was looking for a way to do it without changing anything in the original CMakeLists.txt. I thought -D overwrites the variable and I can clearly see it in the case of that library. Is this something I need to pick up with the devlopers? – Robert Manson-Sawko Jan 26 '18 at 14:15
  • 1
    `-D` 'predefines' the variable. Probably the developers are overwriting whatever you pass via command line. – sbabbi Jan 26 '18 at 14:21