Q:

Compiler flags for C++11

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?

A:

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.

