cmakecudac++20ubuntu-20.04nvcc

How to compile C++20 CUDA program on Ubuntu 20?


I am trying to build a program with some CUDA files using C++20 language standard on Ubuntu 20.04 LTS (Focal Fossa) with nvidia/cuda:12.1.0-devel-ubuntu20.04:

The CUDA compiler identification is NVIDIA 12.1.105

First, I tried to add in CMakeLists.txt the lines

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

but it seems to have no effect. The compiler rejected even C++17 features, e.g.

warning #3033-D: inline variables are a C++17 feature

The second attempt was to add compiler flag directly:

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --std c++20")

and I got

nvcc warning : The -std=c++20 flag is not supported with the configured host compiler. Flag will be ignored.

Which is very surprising considering that I tried both GCC 10 and Clang 11 as CMAKE_CXX_COMPILER.

The advice from the comments to write

set(CMAKE_CUDA_STANDARD 20)

does not work unfortunately on Ubuntu 20 with its default Cmake 3.16:

CUDA_STANDARD is set to invalid value '20'

I would prefer manually passing command-option to the compiler to forcing Cmake update.


Solution

  • The CMake documentation says that the first version to really support 20 for CUDA is 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

    Try updating your version of CMake. For docker images, like the ones you are using, I sometimes have to download the binary and install it:

        - 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