cmake3.9之后开始支持cuda和c++混合编程,这里给几个cmake的例子:
最简单的Hello Cuda:
cmake_minimum_required(VERSION 3.20)
project(Hello)
set(CMAKE_CXX_STANDARD 14)
find_package(CUDA REQUIRED)
cuda_add_executable(Hello main.cu)
一个复杂一点的,Cuda编译出库的例子:
cmake_minimum_required(VERSION 3.20)
project(DiffRast LANGUAGES CUDA CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_library(CudaRasterizer
cuda_rasterizer/backward.h
cuda_rasterizer/backward.cu
cuda_rasterizer/forward.h
cuda_rasterizer/forward.cu
cuda_rasterizer/auxiliary.h
cuda_rasterizer/rasterizer_impl.cu
cuda_rasterizer/rasterizer_impl.h
cuda_rasterizer/rasterizer.h
)
set_target_properties(CudaRasterizer PROPERTIES CUDA_ARCHITECTURES "70;75;86")
target_include_directories(CudaRasterizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/cuda_rasterizer)
target_include_directories(CudaRasterizer PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
一个复杂一点儿,C++引用cuda编译出exe的例子
cmake_minimum_required (VERSION 3.8)
project ("algMegaClassifyConfig" LANGUAGES CXX CUDA)
SET(INC_DIR ./cuda11.5/include
./opencv-452/include
./cuStudy)
SET(LIB_DIR ./cuda11.5/libs
./opencv-452/libs)
#link_libraries(onnxruntime)
set(CUDA_LIBS cublas cudart curand cufft)
set(NPP_LIBS nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps )
set(OPENCV_LIBS opencv_world452d)
set(LINK_LIBS ${CUDNN_LIB} ${NPP_LIBS} ${CUDA_LIBS} ${OPENCV_LIBS} )
#aux_source_directory( ${COMMON_ROOT}/src COMMON_SRC_LIST)
#SET(SRC_LIST ${COMMON_SRC_LIST} "src/location.cpp")
include_directories(${INC_DIR})
link_directories(${LIB_DIR})
link_libraries(${LINK_LIBS})
ADD_EXECUTABLE(CudaSamples "./cuStudy/post_gpu.cpp" "./cuStudy/gpu_decode.cu" )