FindCXX11.cmake 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # - Finds if the compiler has C++11 support
  2. # This module can be used to detect compiler flags for using C++11, and checks
  3. # a small subset of the language.
  4. #
  5. # The following variables are set:
  6. # CXX11_FLAGS - flags to add to the CXX compiler for C++11 support
  7. # CXX11_FOUND - true if the compiler supports C++11
  8. #
  9. if(CXX11_FLAGS)
  10. set(CXX11_FOUND TRUE)
  11. return()
  12. endif()
  13. include(CheckCXXSourceCompiles)
  14. include(FindPackageHandleStandardArgs)
  15. if(MSVC)
  16. set(CXX11_FLAG_CANDIDATES
  17. " "
  18. )
  19. else()
  20. set(CXX11_FLAG_CANDIDATES
  21. #gcc
  22. "-std=gnu++11"
  23. "-std=gnu++0x"
  24. #Gnu and Intel Linux
  25. "-std=c++11"
  26. "-std=c++0x"
  27. #Microsoft Visual Studio, and everything that automatically accepts C++11
  28. " "
  29. #Intel windows
  30. "/Qstd=c++11"
  31. "/Qstd=c++0x"
  32. )
  33. endif()
  34. set(CXX11_TEST_SOURCE
  35. "
  36. int main()
  37. {
  38. int n[] = {4,7,6,1,2};
  39. int r;
  40. auto f = [&](int j) { r = j; };
  41. for (auto i : n)
  42. f(i);
  43. return 0;
  44. }
  45. ")
  46. foreach(FLAG ${CXX11_FLAG_CANDIDATES})
  47. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  48. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  49. unset(CXX11_FLAG_DETECTED CACHE)
  50. message(STATUS "Try C++11 flag = [${FLAG}]")
  51. check_cxx_source_compiles("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
  52. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  53. if(CXX11_FLAG_DETECTED)
  54. set(CXX11_FLAGS_INTERNAL "${FLAG}")
  55. break()
  56. endif(CXX11_FLAG_DETECTED)
  57. endforeach(FLAG ${CXX11_FLAG_CANDIDATES})
  58. set(CXX11_FLAGS "${CXX11_FLAGS_INTERNAL}" CACHE STRING "C++11 Flags")
  59. find_package_handle_standard_args(CXX11 DEFAULT_MSG CXX11_FLAGS)
  60. mark_as_advanced(CXX11_FLAGS)