Skip to content

Support & Doc CMAKE_CXX_STANDARD #2184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions docs/compiling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,14 @@ on the target compiler, falling back to C++11 if C++14 support is not
available. Note, however, that this default is subject to change: future
pybind11 releases are expected to migrate to newer C++ standards as they become
available. To override this, the standard flag can be given explicitly in
``PYBIND11_CPP_STANDARD``:
`CMAKE_CXX_STANDARD <https://cmake.org/cmake/help/v3.17/variable/CMAKE_CXX_STANDARD.html>`_:

.. code-block:: cmake

# Use just one of these:
# GCC/clang:
set(PYBIND11_CPP_STANDARD -std=c++11)
set(PYBIND11_CPP_STANDARD -std=c++14)
set(PYBIND11_CPP_STANDARD -std=c++1z) # Experimental C++17 support
# MSVC:
set(PYBIND11_CPP_STANDARD /std:c++14)
set(PYBIND11_CPP_STANDARD /std:c++latest) # Enables some MSVC C++17 features
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17) # Experimental C++17 support

add_subdirectory(pybind11) # or find_package(pybind11)

Expand Down
4 changes: 2 additions & 2 deletions tools/pybind11Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#
# Python headers, libraries (as needed by platform), and the C++ standard
# are attached to the target. Set PythonLibsNew variables to influence
# python detection and PYBIND11_CPP_STANDARD (-std=c++11 or -std=c++14) to
# influence standard setting. ::
# python detection and CMAKE_CXX_STANDARD (11 or 14) to influence standard
# setting. ::
#
# find_package(pybind11 CONFIG REQUIRED)
# message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
Expand Down
32 changes: 24 additions & 8 deletions tools/pybind11Tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,40 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
include(CheckCXXCompilerFlag)
include(CMakeParseArguments)

# Use the language standards abstraction if CMake supports it with the current compiler
if(NOT CMAKE_VERSION VERSION_LESS 3.1)
if(NOT CMAKE_CXX_STANDARD)
if(CMAKE_CXX14_STANDARD_COMPILE_OPTION)
set(CMAKE_CXX_STANDARD 14)
elseif(CMAKE_CXX11_STANDARD_COMPILE_OPTION)
set(CMAKE_CXX_STANDARD 11)
endif()
endif()
if(CMAKE_CXX_STANDARD)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
endif()

# Fall back to heuristics
if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD)
if(NOT MSVC)
if(MSVC)
set(PYBIND11_CPP_STANDARD /std:c++14)
else()
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)

if (HAS_CPP14_FLAG)
if(HAS_CPP14_FLAG)
set(PYBIND11_CPP_STANDARD -std=c++14)
else()
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
if (HAS_CPP11_FLAG)
if(HAS_CPP11_FLAG)
set(PYBIND11_CPP_STANDARD -std=c++11)
else()
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
endif()
endif()
elseif(MSVC)
set(PYBIND11_CPP_STANDARD /std:c++14)
endif()

if(NOT PYBIND11_CPP_STANDARD)
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
endif()
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
"C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
endif()
Expand Down