我正在尝试在 Ubuntu 20.04 LTS (Focal Fossa) 上使用 C++20 语言标准使用一些 CUDA 文件构建一个程序 nvidia/cuda:12.1.0-devel-ubuntu20.04
:
The CUDA compiler identification is NVIDIA 12.1.105
首先,我尝试在 CMakeLists.txt 中添加这些行
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
但是好像没有什么效果。编译器甚至拒绝了 C++17 功能,例如
warning #3033-D: inline variables are a C++17 feature
第二次尝试是直接添加编译器标志:
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --std c++20")
我得到了
nvcc warning : The -std=c++20 flag is not supported with the configured host compiler. Flag will be ignored.
考虑到我尝试了 GCC 10 和 Clang 11 作为 CMAKE_CXX_COMPILER
,这是非常令人惊讶的.
评论中的建议写
set(CMAKE_CUDA_STANDARD 20)
不幸的是,它在默认 Cmake 3.16 的 Ubuntu 20 上不起作用:
CUDA_STANDARD is set to invalid value '20'
我更喜欢手动将命令选项传递给编译器,而不是强制 Cmake 更新。
最佳答案
CMake 文档称,第一个真正支持 CUDA 20 的版本是 3.18。
This property specifies the CUDA/C++ standard whose features are requested to build this target. For some compilers, this results in adding a flag such as -std=gnu++11 to the compile line. Supported values are:
...
20
New in version 3.12.
CUDA C++20. While CMake 3.12 and later recognize
20
as a valid value, CMake 3.18 was the first version to include support for any compiler.
https://cmake.org/cmake/help/latest/prop_tgt/CUDA_STANDARD.html
尝试更新您的 CMake 版本。 对于 Docker 镜像,例如您正在使用的镜像,我有时必须下载二进制文件并安装它:
- wget https://github.com/Kitware/CMake/releases/download/v3.27.0-rc3/cmake-3.27.0-rc3-linux-x86_64.sh --no-verbose
- sh ./cmake-3.27.0-rc3-linux-x86_64.sh --skip-license --prefix=/usr
- cmake --version
https://gitlab.com/correaa/boost-multi/-/blob/master/.gitlab-ci.yml?ref_type=heads#L402-403
关于cmake - 如何在Ubuntu 20上编译C++20 CUDA程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77170793/