在cmake中指定clang编译cuda文件

发布于:2025-06-26 ⋅ 阅读:(20) ⋅ 点赞:(0)

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.cppfoo.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

网站公告

今日签到

点亮在社区的每一天
去签到