Reputation: 597
I'm trying to write a CMakeFiles.txt (never done it before) and I'm not sure what compiler flag to use for C++11. I use GCC 4.8.2 and the flag is std=c++0x but I'm not sure what to do about other compilers. I don't suppose they all use that flag, I believe MinGW-TDM uses std=c++11, what would the correct way to ensure the compiler uses c++11 be?
Upvotes: 5
Views: 4203
Reputation: 23461
In my project I just try to add -std=c++11 and if that does not work I try -std=c++0x. When both fail, I just leave it alone. For me that works great but I have never tried a Windows compiler. Here an example code:
# test for C++11 flags
include(TestCXXAcceptsFlag)
if(NOT DISABLE_GXX0XCHECK)
# try to use compiler flag -std=c++11
check_cxx_accepts_flag("-std=c++11" CXX_FLAG_CXX11)
endif(NOT DISABLE_GXX0XCHECK)
if(CXX_FLAG_CXX11)
[add flag -std=c++11 where needed]
else()
if(NOT DISABLE_GXX0XCHECK)
# try to use compiler flag -std=c++0x for older compilers
check_cxx_accepts_flag("-std=c++0x" CXX_FLAG_CXX0X)
endif(NOT DISABLE_GXX0XCHECK)
if(CXX_FLAG_CXX0X)
[add flag -std=c++11 where needed]
...
Mabye stackoverflow.com/q/10984442 does help you, too. Erik Sjölund suggest to have a look at CMake nightly build's FindCXXFeatures.cmake.
Edit: I tried this with Microsoft's Visual C++ and my solution has a flaw: The compiler emits a warning about the unrecognized flag. This means CMake detects the flag as correct and adds it. Thus one gets this warning for every executable. Adding -Werror, or whatever the flag for Visual C++ is, should help.
Upvotes: 3
Reputation: 11408
With the upcoming CMake release 3.1.0 (now available as release candidate 2) the new way is to use the CMake command target_compile_features to specify what C++ features are being used. CMake will from this information make sure the C++ compiler is invoked with the correct command line flags (e.g. -std=c++11).
For example, this C++ program with the filename main.cc makes use of the C++ features: cxx_strong_enums, cxx_constexpr, cxx_auto_type
#include <cstdlib>
int main(int argc, char *argv[]) {
enum class Color { Red, Orange, Yellow, Green, Blue, Violet };
constexpr float a = 3.1415f;
auto b = a;
return EXIT_SUCCESS;
}
This CMakeLists.txt file would build it
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(foobar CXX)
add_executable(foobar main.cc)
set(needed_features
cxx_strong_enums
cxx_constexpr
cxx_auto_type)
target_compile_features(foobar PRIVATE ${needed_features})
As of today (November 21, 2014) CMake version 3.1.0 has not yet been released. It is right now available as release candidate 2. Note, this functionality has not yet been released in a stable CMake release, so I guess, only use this if you like to experiment.
Upvotes: 0
Reputation: 17131
You need to set your flags manually for each compiler you're targeting:
IF (CMAKE_COMPILER_IS_GNUCXX)
SET (CMAKE_CXX_FLAGS "-std=gnu++11")
ELSEIF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
SET (CMAKE_CXX_FLAGS "-std=c++11")
ELSEIF (MSVC)
# On by default
ENDIF ()
As a for instance.
Upvotes: 1