在cmake中指定clang编译cuda文件
cuda
支持 C++
语法,因此即使没有 nvcc
,某些定制化的 clang
编译器也能编译 cuda
文件,下面就演示一下如何在 CMakeLists.txt
文件中指定使用 clang
来编译 cuda
文件:
cmake_minimum_required(VERSION 3.20)set(CMAKE_C_COMPILER "/usr/lib/llvm-13/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/lib/llvm-13/bin/clang")project(Test)add_executable(test a.cpp foo.cu)
set_source_files_properties(foo.cu PROPERTIES LANGUAGE CXX)
如上所示,还需要自定义两个源文件:a.cpp
,foo.cu
,并且 a.cpp
调用了 foo.cu
中实现的某个函数,则编译过程如下:
$ mkdir build && cd build
$ cmake ..
-- The C compiler identification is Clang 13.0.1
-- The CXX compiler identification is Clang 13.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/lib/llvm-13/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/lib/llvm-13/bin/clang - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.6s)
-- Generating done (0.0s)
-- Build files have been written to: /home/xxx/test/build
$ make
[ 66%] Building CXX object CMakeFiles/test.dir/a.cpp.o
[ 66%] Building CXX object CMakeFiles/test.dir/foo.cu.o
[100%] Linking CXX executable test
[100%] Built target test