@@ -18,26 +18,28 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
1818include (CheckCXXCompilerFlag)
1919include (CMakeParseArguments)
2020
21- if (NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD)
22- if (NOT MSVC )
23- check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
24-
25- if (HAS_CPP14_FLAG)
26- set (PYBIND11_CPP_STANDARD -std=c++14)
27- else ()
28- check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
29- if (HAS_CPP11_FLAG)
30- set (PYBIND11_CPP_STANDARD -std=c++11)
21+ if (NOT CMAKE_CXX_STANDARD)
22+ if (NOT PYBIND11_CPP_STANDARD AND CMAKE_VERSION VERSION_LESS 3.8)
23+ if (NOT MSVC )
24+ check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
25+
26+ if (HAS_CPP14_FLAG)
27+ set (PYBIND11_CPP_STANDARD -std=c++14)
3128 else ()
32- message (FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!" )
29+ check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
30+ if (HAS_CPP11_FLAG)
31+ set (PYBIND11_CPP_STANDARD -std=c++11)
32+ else ()
33+ message (FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!" )
34+ endif ()
3335 endif ()
36+ elseif (MSVC )
37+ set (PYBIND11_CPP_STANDARD /std:c++14)
3438 endif ()
35- elseif (MSVC )
36- set (PYBIND11_CPP_STANDARD /std:c++14)
39+
40+ set (PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
41+ "C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
3742 endif ()
38-
39- set (PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
40- "C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
4143endif ()
4244
4345# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and
@@ -173,7 +175,11 @@ function(pybind11_add_module target_name)
173175 endif ()
174176
175177 # Make sure C++11/14 are enabled
176- target_compile_options (${target_name} PUBLIC ${PYBIND11_CPP_STANDARD} )
178+ if (PYBIND11_CPP_STANDARD OR ${CMAKE_VERSION} VERSION_LESS 3.8)
179+ target_compile_options (${target_name} PUBLIC ${PYBIND11_CPP_STANDARD} )
180+ else ()
181+ target_compile_features (${target_name} PUBLIC cxx_std_11) # Will be 14 if parent project requests 14
182+ endif ()
177183
178184 if (ARG_NO_EXTRAS)
179185 return ()
0 commit comments