CMake未能告诉MSVC使用C++11?

huangapple go评论66阅读模式
英文:

CMake failing to tell MSVC to use C++11?

问题

I'm using CMake to generate the build system of a library of mine. I have some example programs, while I build with the language standard set to C++11:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Now, for a Windows build, I (or rather GitHub) is using MSVC 16 on Windows 10. For this program:

add_executable(bandwidthtest modified_cuda_samples/bandwidthtest/bandwidthtest.cpp)

I get this compilation command generated by CMake (I think it's CMake 3.26.3; lines broken for readability):

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\CL.exe
/c /I"D:\a\cuda-api-wrappers\cuda-api-wrappers\src"
/I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include" /nologo /W4 
/WX- /diagnostics:column /O2 /Ob2 /D _MBCS /D WIN32 /D _WINDOWS /D NDEBUG 
/D _CRT_SECURE_NO_DEPRECATE /D _CRT_SECURE_NO_WARNINGS 
/D _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING /D "CMAKE_INTDIR=\"Release\""
/Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"bandwidthtest.dir\Release\" /Fd"bandwidthtest.dir\Release\vc142.pdb" /external:W0 
/Gd /TP /errorReport:queue  
/external:I "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/include" /Zc:__cplusplus 
"D:\a\cuda-api-wrappers\cuda-api-wrappers\examples\modified_cuda_samples\bandwidthtest\bandwidthtest.cpp"

So, this doesn't have an /std option. It does have /Zc:__cplusplus, but I added that one myself (I think CMake should have added it as well, but that's a known open issue).

Given that MSVC does not default to using C++11 as the language standard (to the best of my recollection) - why does CMake not add /std:c++11? And if it's because of some configuration problem - why doesn't it say there's a problem?

Note: This is not a question about Visual Studio, the question is about CMake's behavior.

英文:

I'm using CMake to generate the build system of a library of mine. I have some example programs, while I build with the language standard set to C++11:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Now, for a Windows build, I (or rather GitHub) is using MSVC 16 on Windows 10. For this program:

add_executable(bandwidthtest modified_cuda_samples/bandwidthtest/bandwidthtest.cpp)

I get this compilation command generated by CMake (I think it's CMake 3.26.3; lines broken for readability):

C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\VC\Tools\MSVC.29.30133\bin\HostX64\x64\CL.exe
/c /I"D:\a\cuda-api-wrappers\cuda-api-wrappers\src"
/I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include" /nologo /W4 
/WX- /diagnostics:column /O2 /Ob2 /D _MBCS /D WIN32 /D _WINDOWS /D NDEBUG 
/D _CRT_SECURE_NO_DEPRECATE /D _CRT_SECURE_NO_WARNINGS 
/D _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING /D "CMAKE_INTDIR=\"Release\"" 
/Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"bandwidthtest.dir\Release\\" /Fd"bandwidthtest.dir\Release\vc142.pdb" /external:W0 
/Gd /TP /errorReport:queue  
/external:I "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7/include" /Zc:__cplusplus 
"D:\a\cuda-api-wrappers\cuda-api-wrappers\examples\modified_cuda_samples\bandwidthtest\bandwidthtest.cpp"

So, this doesn't have an /std option. It does have /Zc:__cplusplus, but I added that one myself (I think CMake should have added it as well, but that's a known open issue).

Given that MSVC does not default to using C++11 as the language standard (to the best of my recollection) - why does CMake not add /std:c++11? And if it's because of some configuration problem - why doesn't it say there's a problem?

Note: This is not a question about Visual Studio, the question is about CMake's behavior.

答案1

得分: 4

看起来MSVC并不正式支持C++11。这个关于/std开关的页面显示它可以使用:/std:c++14/std:c++17/std:c++20,以及C语言的值(截止到写作时)。但是没有/std:c++11 CMake未能告诉MSVC使用C++11?

然而,这并没有完全回答这个问题。CMake应该以不同的方式处理这个问题,而它没有这样做是一个bug,依我个人看来。

英文:

It looks like MSVC doesn't officially support C++11. This page about the /std switch shows us it can take: /std:c++14, /std:c++17, /std:c++20, and C-language values (as of the time of writing). But no /std:c++11 CMake未能告诉MSVC使用C++11?

This does not fully answer the question though. CMake should have dealt with this differently, and the fact that it doesn't is a bug, IMNSHO.

答案2

得分: 0

Microsoft Visual Studio 2019 不支持 C++11: /std (指定语言标准版本) 缺少 /std:c++11

> Visual Studio 2017 和更高版本中的 Microsoft C++ 编译器不支持早于 C++14 的 C++ 标准模式 (/std:c++14)。

英文:

Microsoft Visual Studio 2019 does not support C++11: /std (Specify Language Standard Version) is missing /std:c++11.

> The Microsoft C++ compiler in Visual Studio 2017 and later versions doesn't support C++ standards modes earlier than C++14 (/std:c++14).

huangapple
  • 本文由 发表于 2023年4月20日 06:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定