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. if(MSVC)
  15. set(CXX11_FLAG_CANDIDATES
  16. " "
  17. )
  18. else()
  19. set(CXX11_FLAG_CANDIDATES
  20. #gcc
  21. "-std=gnu++11"
  22. "-std=gnu++0x"
  23. #Gnu and Intel Linux
  24. "-std=c++11"
  25. "-std=c++0x"
  26. #Microsoft Visual Studio, and everything that automatically accepts C++11
  27. " "
  28. #Intel windows
  29. "/Qstd=c++11"
  30. "/Qstd=c++0x"
  31. )
  32. endif()
  33. set(CXX11_TEST_SOURCE
  34. "
  35. int main()
  36. {
  37. int n[] = {4,7,6,1,2};
  38. int r;
  39. auto f = [&](int j) { r = j; };
  40. for (auto i : n)
  41. f(i);
  42. return 0;
  43. }
  44. ")
  45. foreach(FLAG ${CXX11_FLAG_CANDIDATES})
  46. set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  47. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  48. unset(CXX11_FLAG_DETECTED CACHE)
  49. message(STATUS "Try C++11 flag = [${FLAG}]")
  50. check_cxx_source_compiles("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
  51. set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
  52. if(CXX11_FLAG_DETECTED)
  53. set(CXX11_FLAGS_INTERNAL "${FLAG}")
  54. break()
  55. endif(CXX11_FLAG_DETECTED)
  56. endforeach(FLAG ${CXX11_FLAG_CANDIDATES})
  57. set(CXX11_FLAGS "${CXX11_FLAGS_INTERNAL}" CACHE STRING "C++11 Flags")
  58. include(FindPackageHandleStandardArgs)
  59. find_package_handle_standard_args(CXX11 DEFAULT_MSG CXX11_FLAGS)
  60. mark_as_advanced(CXX11_FLAGS)