【cmake开发(12)】Cmake同时编译cpp文件和cu文件

1 、首先、利用cmake 编译纯 cu 代码

文件目录结构如下:

/project_root_path
----/build
----CMakeLists.txt
----main.cu

1.1. 使用find_package

如果CMake的版本小于3.10,可以在CMakeLists.txt文件中使用find_package来导入CUDA包,然后就可以使用cuda_add_executable()或者cuda_add_library()来编译CUDA可执行文件或者库文件了。

cmake_minimum_required(VERSION 3.8)
project(CUDA_TEST)

find_package(CUDA REQUIRED)

message(STATUS "cuda version: " ${CUDA_VERSION_STRING})
include_directories(${CUDA_INCLUDE_DIRS})

cuda_add_executable(cuda_test cuda_test.cu)
target_link_libraries(cuda_test ${CUDA_LIBRARIES})

其中变量CUDA_VERSION_STRING表示CUDA的版本号,CUDA_INCLUDE_DIRS表示CUDA头文件存放的目录,CUDA_LIBRARIES表示CUDA的库文件。更多说明可以参考CMake的官方文档:

https://cmake.org/cmake/help/latest/module/FindCUDA.html

关键两步:
1.add_executable指令换为cuda_add_executable
2.在cuda_add_executable执行前添加set_source_files_properties(${主c++文件名} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)

部分代码:

set(CUDA_GEN_CODE "-gencode=arch=compute_75,code=sm_75")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}  -std=c++11 -O0 -Xcompiler -fPIC -g -w ${CUDA_GEN_CODE}")
set_source_files_properties(yolov5.cpp PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
cuda_add_executable(yolov5 ${PROJECT_SOURCE_DIR}/calibrator.cpp ${PROJECT_SOURCE_DIR}/yolov5.cpp pre.cu)

1.2.添加CUDA编程语言支持

在3.10及以上版本的CMake中,find_package的方式已经被弃用(可以用但不推荐),要编译CUDA代码可以CMakeLists.txt文件中添加对CUDA编程语言的支持。如果程序中CUDA代码是可选的,那么可以在CMakeLists.txt文件中使用下面的语句进行使能:

enable_language(CUDA)

20231018增加Note:如果报错:Failed to detect a default CUDA architecture.CMAKE_CUDA_ARCHITECTURES must be non-empty if set.
手动设置架构(参考:https://avmedia.0voice.com/?id=40747)

set(CMAKE_CUDA_ARCHITECTURES "61;75")
set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc" )
set(CUDA_GEN_CODE "-gencode=arch=compute_75,code=sm_75")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}  -std=c++11 -O0 -Xcompiler -fPIC -g -w
${CUDA_GEN_CODE}")

Configure the compute capability matched with your nvidia graphics card in Makefile/CMakeLists.txt

  • e.g. -gencode=arch=compute_75,code=sm_75. If you are using 3080Ti, that should be gencode=arch=compute_86,code=sm_86

  • reference for the table for GPU Compute Capability: https://developer.nvidia.com/cuda-gpus#compute

如果CUDA代码是必须的,那么就需要像下面这样进行设置,表示在项目CUDA_TEST中要用到CUDA和C++两种编程语言:

project(CUDA_TEST LANGUAGES CUDA CXX)

可以通过CheckLanuage判断CUDA是否可用

include(CheckLanguage)
check_language(CUDA)

然后就可以跟编译普通C++代码一样用add_executable编译可执行文件了:

cmake_minimum_required(VERSION 3.10)
project(CUDA_TEST LANGUAGES CUDA CXX)

include(CheckLanguage)
check_language(CUDA)

add_executable(cuda_test cuda_test.cu)

2 Cmake同时编译cpp文件和cu文件

如果项目目录下既有.cu文件又有.cpp文件,先将.cu编译为动态库,再由.cpp调用

目录结构:

/project_root_path
----/build
----/cuda
--------CMakeLists.txt
--------deploy.cu
--------deploy.h
----CMakeLists.txt
----main.cpp

最外层CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(test)
enable_language(CXX)
add_subdirectory(./cuda)

aux_source_directory(. SRC)
add_executable(test ${SRC})
target_link_libraries(test gpu) 
## 链接 gpu.lib 文件,不需要写绝对路径 

cuda/CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)
project(gpu_lib)
enable_language(CUDA)

aux_source_directory(. SRC_CUDA)
add_library(gpu_lib SHARED ${SRC_CUDA})

参考

链接:https://blog.csdn.net/qq_41955812/article/details/120746837
https://zhuanlan.zhihu.com/p/620194070?utm_id=0
https://blog.csdn.net/qq_43596950/article/details/130654274

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值