Skip to content

Commit 77710ff

Browse files
committed
Make PYBIND11_CPP_STANDARD work under MSVC
Under MSVC we were ignoring PYBIND11_CPP_STANDARD and simply not passing any standard (which makes MSVC default to its C++14 mode). MSVC 2015u3 added the `/std:c++14` and `/std:c++latest` flags; the latter, under MSVC 2017, enables some C++17 features (such as `std::optional` and `std::variant`), so it is something we need to start supporting under MSVC. This makes the PYBIND11_CPP_STANDARD cmake variable work under MSVC, defaulting it to /std:c++14 (matching the default -std=c++14 for non-MSVC). It also adds a new appveyor test running under MSVC 2017 with /std:c++latest, which runs (and passes) the `std::optional`/`std::variant` tests. Also updated the documentation to clarify the c++ flags and add show MSVC flag examples.
1 parent ca0e82b commit 77710ff

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

.appveyor.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ platform:
1111
environment:
1212
matrix:
1313
- CONDA: 36
14+
CPP: 14
1415
- CONDA: 27
16+
CPP: 14
17+
- CONDA: 36
18+
CPP: latest
1519
matrix:
1620
exclude:
1721
- image: Visual Studio 2015
1822
platform: x86
23+
- image: Visual Studio 2015
24+
CPP: latest
25+
- image: Visual Studio 2017
26+
CPP: latest
27+
platform: x86
1928
install:
2029
- ps: |
2130
if ($env:PLATFORM -eq "x64") { $env:CMAKE_ARCH = "x64" }
@@ -37,7 +46,7 @@ install:
3746
7z x 3.3.3.zip -y > $null
3847
$env:CMAKE_INCLUDE_PATH = "eigen-eigen-67e894c6cd8f"
3948
build_script:
40-
- cmake -G "%CMAKE_GENERATOR%" -A "%CMAKE_ARCH%" -DPYBIND11_WERROR=ON -DCMAKE_SUPPRESS_REGENERATION=1
49+
- cmake -G "%CMAKE_GENERATOR%" -A "%CMAKE_ARCH%" -DPYBIND11_CPP_STANDARD=/std:c++%CPP% -DPYBIND11_WERROR=ON -DCMAKE_SUPPRESS_REGENERATION=1
4150
- set MSBuildLogger="C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
4251
- cmake --build . --config Release --target pytest -- /v:m /logger:%MSBuildLogger%
4352
- cmake --build . --config Release --target test_cmake_build -- /v:m /logger:%MSBuildLogger%

docs/compiling.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,28 @@ regular LTO if ``-flto=thin`` is not available.
9292
Configuration variables
9393
-----------------------
9494

95-
By default, pybind11 will compile modules with the latest C++ standard
96-
available on the target compiler. To override this, the standard flag can
97-
be given explicitly in ``PYBIND11_CPP_STANDARD``:
95+
By default, pybind11 will compile modules with the C++14 standard, if available
96+
on the target compiler, falling back to C++11 if C++14 support is not
97+
available. Note, however, that this default is subject to change: future
98+
pybind11 releases are expected to migrate to newer C++ standards as they become
99+
available. To override this, the standard flag can be given explicitly in
100+
``PYBIND11_CPP_STANDARD``:
98101

99102
.. code-block:: cmake
100103
104+
# Use just one of these:
105+
# GCC/clang:
101106
set(PYBIND11_CPP_STANDARD -std=c++11)
107+
set(PYBIND11_CPP_STANDARD -std=c++14)
108+
set(PYBIND11_CPP_STANDARD -std=c++1z) # Experimental C++17 support
109+
# MSVC:
110+
set(PYBIND11_CPP_STANDARD /std:c++14)
111+
set(PYBIND11_CPP_STANDARD /std:c++latest) # Enables some MSVC C++17 features
112+
102113
add_subdirectory(pybind11) # or find_package(pybind11)
103114
104115
Note that this and all other configuration variables must be set **before** the
105-
call to ``add_subdiretory`` or ``find_package``. The variables can also be set
116+
call to ``add_subdirectory`` or ``find_package``. The variables can also be set
106117
when calling CMake from the command line using the ``-D<variable>=<value>`` flag.
107118

108119
The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION``

tools/pybind11Tools.cmake

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ include(CheckCXXCompilerFlag)
1919
include(CMakeParseArguments)
2020

2121
function(select_cxx_standard)
22-
if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
23-
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
24-
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
25-
26-
if (HAS_CPP14_FLAG)
27-
set(PYBIND11_CPP_STANDARD -std=c++14)
28-
elseif (HAS_CPP11_FLAG)
29-
set(PYBIND11_CPP_STANDARD -std=c++11)
30-
else()
31-
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
22+
if(NOT PYBIND11_CPP_STANDARD)
23+
if(NOT MSVC)
24+
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
25+
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
26+
27+
if (HAS_CPP14_FLAG)
28+
set(PYBIND11_CPP_STANDARD -std=c++14)
29+
elseif (HAS_CPP11_FLAG)
30+
set(PYBIND11_CPP_STANDARD -std=c++11)
31+
else()
32+
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
33+
endif()
34+
elseif(MSVC)
35+
set(PYBIND11_CPP_STANDARD /std:c++14)
3236
endif()
3337

3438
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
35-
"C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)
39+
"C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
3640
endif()
3741
endfunction()
3842

@@ -162,10 +166,8 @@ function(pybind11_add_module target_name)
162166
endif()
163167

164168
select_cxx_standard()
165-
if(NOT MSVC)
166-
# Make sure C++11/14 are enabled
167-
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
168-
endif()
169+
# Make sure C++11/14 are enabled
170+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
169171

170172
if(ARG_NO_EXTRAS)
171173
return()

0 commit comments

Comments
 (0)