Compiler Flags for C ++ 11

I am trying to write CMakeFiles.txt (I have never done this before) and I'm not sure which compiler flag to use for C ++ 11. I am using GCC 4.8.2 and the flag is std = C ++ 0x, but I'm not sure what to do with other compilers. I don’t think that they all use this flag, I believe that MinGW-TDM uses std = C ++ 11, what would be the right way to ensure that the compiler uses C ++ 11?

+4
source share
4 answers

In my project, I'm just trying to add -std = C ++ 11, and if that doesn't work, I'm trying -std = C ++ 0x. When both fail, I will just leave it alone. This works fine for me, but I have never tried the Windows compiler. Here is a sample 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 fooobar.com/questions/27366/... . CMake, CMake, FindCXXFeatures.cmake.

: Microsoft Visual ++, : . , CMake . , . -Werror, - Visual ++, .

+3

:

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 ()

, ,

+1

CMake 3.1.0 ( 2) CMake target_compile_features, , ++. CMake , ++ (, -std = ++ 11).

, ++ main.cc ++: 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;
}

CMakeLists.txt

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})

To date (November 21, 2014) CMake version 3.1.0 has not yet been released. It is now available as a candidate for release 2. Note: this functionality has not yet been released in the stable release of CMake, so I think you should only use this if you like to experiment.

0
source
set_property(TARGET your_target PROPERTY CXX_STANDARD 11)
0
source

All Articles