cmake - 在 CMake 中指定 CUDA 编译器

标签 cmake clang llvm llvm-clang

我正在尝试基于以下 documentation 使用 clang-3.9 构建一个 cuda 项目.但我想使用 cmake 来构建我的项目。我已经分别将 CMAKE_CC_COMPILER 和 CMAKE_CXX_COMPILER 设置为 clang 和 clang++ 。

但问题是,如果我使用

file(GLOB_RECURSE CUDA_SOURCES "./*.cu")
CUDA_ADD_LIBRARY(Benchmarks_CUDA ${CUDA_SOURCES})

在我的 CMakeList.txt 中,默认情况下 cmake 将使用 nvcc 编译器。但是我想使用 clang 来编译 .cu 文件。

相反,如果我使用
add_library (Benchmarks_CUDA ${CUDA_SOURCES})

然后我会得到一个错误
CMake Error: Cannot determine link language for target "Benchmarks_CUDA".
CMake Error: CMake can not determine linker language for target: Benchmarks_CUDA

有人可以告诉我如何使用 cmake 使用 clang 构建 .cu 文件。

最佳答案

恐怕我没有四年前能帮到你的答案。但是,通常情况下,使用新的 CMake 版本会显着改善情况。 CMake 3.18 was the first to officially support using Clang to compile CUDA.我试过那个版本,但它不知道如何使用我的 clang++-12安装。它可能是在 CMake 3.18 之后发布的。
不管;在 CMake 3.19+ 上,设置 CMAKE_CUDA_COMPILER与 Clang 12 一起“正常工作”。

首先,这里是 CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(clang-cuda-test LANGUAGES CUDA)

add_executable(
  vectorAdd
  # Sources
  vectorAdd.cu
  # Headers
  helper_cuda.h
  helper_string.h
)
target_include_directories(vectorAdd PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
我已经从 CUDA 11 示例中复制了各种源文件。所以我将 Clang 12 设置为我的编译器(但请注意,我的 CUDA 11 安装对于它来说太新了,这就是我收到警告的原因):
alex@alex-ubuntu:~/test$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CUDA_COMPILER=clang++-12
-- The CUDA compiler identification is Clang 12.0.1
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /usr/bin/clang++-12 - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/build
alex@alex-ubuntu:~/test$ cmake --build build/ -- -v
[1/2] /usr/bin/clang++-12   -I../ -O3 -DNDEBUG --cuda-gpu-arch=sm_52 --cuda-path=/usr/local/cuda -MD -MT CMakeFiles/vectorAdd.dir/vectorAdd.cu.o -MF CMakeFiles/vectorAdd.dir/vectorAdd.cu.o.d -x cuda -c ../vectorAdd.cu -o CMakeFiles/vectorAdd.dir/vectorAdd.cu.o
clang: warning: Unknown CUDA version. cuda.h: CUDA_VERSION=11030. Assuming the latest supported version 10.1 [-Wunknown-cuda-version]
[2/2] : && /usr/bin/clang++-12  CMakeFiles/vectorAdd.dir/vectorAdd.cu.o -o vectorAdd  -lcudadevrt  -lcudart_static  -lrt  -lpthread  -ldl -L"/usr/local/cuda/lib64" && :
alex@alex-ubuntu:~/test$ ./build/vectorAdd 
[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Copy output data from the CUDA device to the host memory
Test PASSED
Done

关于cmake - 在 CMake 中指定 CUDA 编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444195/

相关文章:

xcode - macOS 'wchar.h' 找不到文件

c++ - 链接 clang/llvm 3.0 库

c++ - 知道在 Visual Studio 上运行的是哪个版本的 c++

c++ - 我可以从多个目标创建一个库吗?

Android Studio CMake 为所有构建变体构建一次

c++ - LLVM 在 std::vector 中存储 Loop*

llvm - 在基于 LLVM 的编译器中如何处理符号表?

c++ - CMake:C++ 包含相对于基本目录

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

c++ - 更改 LLVM 函数的名称