cmake混合编译cuda和c++代码

cmake混合编译cpp和cuda代码

实际背景

在cpp项目中需要调用一些.cu代码,但是两个代码的编译是不同的:cpp代码使用g++编译,cuda代码使用nvvc编译,因此需要分别编译,再链接,同时需要在cuda侧进行一些简单的封装。

简单例子

目录结构

projectDir
├── CMakeLists.txt(final project)
├── src.cpp
├── ...
├── cudaCode
│   ├── CMakeLists.txt(cuda project)
│   ├── cudaTest.cu
│   └── cudaTest.h

其中cudaTest.h声明了函数,cudaTest.cu,定义了声明cuda的函数体,调用了cuda api

CMakeLists for cuda project

将cuda编译成动态库

cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
project (GpuBTree)

find_package(CUDA 8.0 REQUIRED)

option(CMAKE_VERBOSE_MAKEFILE ON)
option(DGTEST,  "DGTEST"  ON)

set(CUDA_NVCC_FLAGS -std=c++11)
set (CMAKE_CXX_STANDARD 11)

if (CUDA_VERBOSE_PTXAS)
  set(VERBOSE_PTXAS --ptxas-options=-v)
endif (CUDA_VERBOSE_PTXAS)

#set(CMAKE_BUILD_TYPE "Release")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(GENCODE_SM30
  -gencode=arch=compute_30,code=sm_30 -gencode=arch=compute_30,code=compute_30)
set(GENCODE_SM35
  -gencode=arch=compute_35,code=sm_35 -gencode=arch=compute_35,code=compute_35)
set(GENCODE_SM37
  -gencode=arch=compute_37,code=sm_37 -gencode=arch=compute_37,code=compute_37)
set(GENCODE_SM50
  -gencode=arch=compute_50,code=sm_50 -gencode=arch=compute_50,code=compute_50)
set(GENCODE_SM60
  -gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_60,code=compute_60)
set(GENCODE_SM61
  -gencode=arch=compute_61,code=sm_61 -gencode=arch=compute_61,code=compute_61)
set(GENCODE_SM70
  -gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_70,code=compute_70)
set(GENCODE_SM71
  -gencode=arch=compute_71,code=sm_71 -gencode=arch=compute_71,code=compute_71)
set(GENCODE_SM75
-gencode=arch=compute_75,code=sm_75 -gencode=arch=compute_75,code=compute_75)

option(GPUBTREE_GENCODE_SM30 "GENCODE_SM30" OFF)
option(GPUBTREE_GENCODE_SM35 "GENCODE_SM35" OFF)
option(GPUBTREE_GENCODE_SM37 "GENCODE_SM37" OFF)
option(GPUBTREE_GENCODE_SM50 "GENCODE_SM50" OFF)
option(GPUBTREE_GENCODE_SM60 "GENCODE_SM60" OFF)
option(GPUBTREE_GENCODE_SM61 "GENCODE_SM61" OFF)
option(GPUBTREE_GENCODE_SM70 "GENCODE_SM70" OFF)
option(GPUBTREE_GENCODE_SM71 "GENCODE_SM71" OFF)
option(GPUBTREE_GENCODE_SM75 "GENCODE_SM75" ON)

if (GPUBTREE_GENCODE_SM30)
  set(GENCODE ${GENCODE} ${GENCODE_SM30})
endif(GPUBTREE_GENCODE_SM30)

if (GPUBTREE_GENCODE_SM35)
  set(GENCODE ${GENCODE} ${GENCODE_SM35})
endif(GPUBTREE_GENCODE_SM35)

if (GPUBTREE_GENCODE_SM37)
  set(GENCODE ${GENCODE} ${GENCODE_SM37})
endif(GPUBTREE_GENCODE_SM37)

if (GPUBTREE_GENCODE_SM50)
  set(GENCODE ${GENCODE} ${GENCODE_SM50})
endif(GPUBTREE_GENCODE_SM50)

if (GPUBTREE_GENCODE_SM60)
  set(GENCODE ${GENCODE} ${GENCODE_SM60})
endif(GPUBTREE_GENCODE_SM60)

if (GPUBTREE_GENCODE_SM61)
  set(GENCODE ${GENCODE} ${GENCODE_SM61})
endif(GPUBTREE_GENCODE_SM61)

if (GPUBTREE_GENCODE_SM70)
  set(GENCODE ${GENCODE} ${GENCODE_SM70})
endif(GPUBTREE_GENCODE_SM70)

if(GPUBTREE_GENCODE_SM71)
  set(GENCODE ${GENCODE} ${GENCODE_SM71})
endif(GPUBTREE_GENCODE_SM71)

if(GPUBTREE_GENCODE_SM75)
  set(GENCODE ${GENCODE} ${GENCODE_SM75})
endif(GPUBTREE_GENCODE_SM75)

set(CUFILES
  cudaTest.h
  cudaTest.cu)

cuda_add_library(cuda_test SHARED
	${CUFILES}
	OPTIONS ${GENCODE} ${VERBOSE_PTXAS})

CMakeLists for final project

在主函数中生成可执行文件,并链接

cmake_minimum_required(VERSION 3.3)

project(GpuTest)

add_subdirectory(cudaCode)
include_directories(cudaCode)

set(test_source_file src.cpp)
add_executable(test_src ${test_source_file})
target_link_libraries(
  test_src
  cuda_test
)

你可能感兴趣的:(c++,cuda)