# CMake + CUDA + Clang fails

**URL:** <https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657>\
**Category:** Code\
**Created:** [August 2, 2023, 1:37am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657 "2023-08-02T01:37:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ptal/32/3661_2.png) [@ptal](https://discourse.cmake.org/u/ptal)\
**Post date:** [August 2, 2023, 1:37am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/1 "2023-08-02T01:37:08Z")

</div>

Hello,

I’m trying to compile [this project](https://github.com/lattice-land/cuda-battery) with Clang instead of NVCC. I am clueless why it is not working and could not find a lot of information online.

I’m running CMake 3.26.4, Clang 15.0.7 and CUDA 12.1.  
Here is what’s happening:

```auto
$ cmake -DCMAKE_BUILD_TYPE=Release -DGPU=ON -Bbuild/gpu-release -DCMAKE_CUDA_COMPILER=clang++ -DCMAKE_CXX_COMPILER=clang++ --trace --trace-redirect=trace.log --trace-expand
Running with trace output on.
Trace will be written to trace.log
Running with expanded trace output on.
-- The CUDA compiler identification is unknown
-- The CXX compiler identification is Clang 15.0.7
-- Detecting CUDA compiler ABI info
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
_CMAKE_CUDA_WHOLE_FLAG
CMake Error at /usr/share/cmake-3.26/Modules/CMakeDetermineCompilerABI.cmake:57 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  /usr/share/cmake-3.26/Modules/CMakeTestCUDACompiler.cmake:19 (CMAKE_DETERMINE_COMPILER_ABI)
  CMakeLists.txt:8 (project)

-- Configuring incomplete, errors occurred!

```

I can’t attach the trace.log because I’m a new user so [here it is](https://sebsauvage.net/paste/?aa09032fa125d297#WNKynBkBTZGvN0iFlbdcOhsu5+kMXSJNWMwhkL2KWCs=).  
Thanks for any help.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 2, 2023, 1:55am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/2 "2023-08-02T01:55:59Z")

</div>

I don’t think that CMake supports `clang++` as a CUDA compiler (hence the “CUDA compiler identification is unknown” output).

[This issue](https://gitlab.kitware.com/cmake/cmake/-/issues/24585) seems relevant.

Cc: @robert.maynard

---

<div class="post-metadata">

**Author:** ![ptal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ptal/32/3661_2.png) [@ptal](https://discourse.cmake.org/u/ptal)\
**Post date:** [August 2, 2023, 2:13am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/3 "2023-08-02T02:13:15Z")

</div>

According to [this](https://cmake.org/cmake/help/latest/release/3.18.html#languages), should be supported from CMake 3.18 onwards?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 2, 2023, 2:40am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/4 "2023-08-02T02:40:06Z")

</div>

Interesting. I wonder if `CUDACC=clang++` is the right thing or whether there’s another wrapper that is expected. Anyways, Rob knows way more about CUDA than I do (I basically just tell CI to turn it on and build/test it).

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [August 2, 2023, 3:07pm UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/5 "2023-08-02T15:07:54Z")

</div>

You need to specify what GPU architecture to compile for via `CMAKE_CUDA_ARCHITECTURES`.

The error logs indicate that is the failure:

```auto
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda-12.1, version 
clang: warning: CUDA version is newer than the latest supported version 11.5 [-Wunknown-cuda-version]
clang: error: GPU arch sm_30 is supported by CUDA versions between 7.0 and 11.0 (inclusive), but installation at /usr/local/cuda-12.1 is ; use '--cuda-path' to specify a different CUDA install, pass a different GPU arch with '--cuda-gpu-arch', or pass '--no-cuda-version-check'

```

Since you are using CUDA 12.1 which doesn’t support the ‘default’ deprecated value of `sm_30`

---

<div class="post-metadata">

**Author:** ![ptal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ptal/32/3661_2.png) [@ptal](https://discourse.cmake.org/u/ptal)\
**Post date:** [August 3, 2023, 2:13am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/6 "2023-08-03T02:13:05Z")

</div>

Thanks. But why does this flag need to be specified? In the CMakeLists I have:

```auto
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
  set(CMAKE_CUDA_ARCHITECTURES native)
endif()

```

Also, I ran into other errors but not because of CMake, just in case someone needs to do the same, I had to install `sudo apt install libstdc++-12-dev` and fix a bug in Clang by modifying ` /usr/lib/clang/15.0.7/include/__clang_cuda_texture_intrinsics.h` (see [this commit](https://github.com/llvm/llvm-project/commit/817340569bf98b696329c53508a0d87cc0daec25#diff-697cdccc0fab849f2172a7978a2c8b7619ac6f7caaba3118aaf9209985ec36c5R669)).  
Anyways, it seems that CUDA 12.1 is only supported starting with Clang 17 so I will have to try with it.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 3, 2023, 12:34pm UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/7 "2023-08-03T12:34:33Z")

</div>

Is that code before `project(… CUDA)` or the `enable_language(CUDA)` call?

---

<div class="post-metadata">

**Author:** ![ptal](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ptal/32/3661_2.png) [@ptal](https://discourse.cmake.org/u/ptal)\
**Post date:** [August 4, 2023, 8:35am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/8 "2023-08-04T08:35:36Z")

</div>

It is before, see [this CMakeLists](https://github.com/lattice-land/cuda-battery/blob/main/CMakeLists.txt)

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [August 4, 2023, 6:31pm UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/9 "2023-08-04T18:31:03Z")

</div>

Don’t set it to `native` till after compiler detection. clang needs an explicit value since it has no intrinsic support for `native`.

---

<div class="post-metadata">

**Author:** ![Burlen\_Loring](https://discourse.cmake.org/user_avatar/discourse.cmake.org/burlen_loring/32/2171_2.png) [@Burlen\_Loring](https://discourse.cmake.org/u/Burlen_Loring)\
**Post date:** [August 21, 2023, 7:40pm UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/10 "2023-08-21T19:40:58Z")

</div>

I hit this as well with clang 18 from git pulled on 8/18.

```auto
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
_CMAKE_CUDA_WHOLE_FLAG

```

In my case in addition to adding to the cmake command line `-DCMAKE_CUDA_ARCHITECTURES=75` I also had to add `-DCMAKE_CUDA_FLAGS=-std=c++17`. The latter being due to the use of -std=gnu++17 by default by clang and inherited by cmake.

Apparently the error complaining about `_CMAKE_CUDA_WHOLE_FLAG` comes up when the clang compiler fails on cmake’s internal try compile, which can be for a number of different reasons.

fedora 37, cmake 3.26.2

---

<div class="post-metadata">

**Author:** ![gazza](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gazza/32/4680_2.png) [@gazza](https://discourse.cmake.org/u/gazza)\
**Post date:** [June 18, 2024, 5:52am UTC](https://discourse.cmake.org/t/cmake-cuda-clang-fails/8657/11 "2024-06-18T05:52:27Z")

</div>

I’ve encountered this issue with Clang 17 after updating the system compiler to GCC 14.

Looking at CMakeConfigureLog.yaml, it’s apparent that when enabling language CUDA cmake’s  
try\_compile() builds a Cuda kernel with clang++ against GNU libstdc++ headers, which has version compatibility risks. I’ve had good results building kernels against libc++ headers using target options, so doing the same via global variables solved this stage for me:

```auto
-DCMAKE_CUDA_FLAGS="-stdlib=libc++ -D_ALLOW_UNSUPPORTED_LIBCPP"

```

manjaro 24, cmake 3.29.3, clang 17.0.6, gcc 14.1.1
