CUDA编程的CMake编写方法

摘要

CUDA编程的CMake编写方法

内容

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
#// 项目名称
PROJECT(Demo)
#// cmake寻找cuda,并且CUDA_ADD_EXECUTABLE等cuda前缀的命令是由cuda提供的,cmake本身不支持
FIND_PACKAGE(CUDA REQUIRED)
if (NOT CUDA_FOUND)
  message(STATUS "CUDA not found. Project will not be built.")
endif(NOT CUDA_FOUND)

#// C++和CUDA的编译参数,可选。如果CMAKE_CXX_FLAGS和CUDA_NVCC_FLAGS有相同参数会报错
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_61,code=sm_61;)
#// 头文件路径
INCLUDE_DIRECTORIES(
    ${PROJECT_SOURCE_DIR}/include
)
#// 库文件路径,按需
LINK_DIRECTORIES(/usr/lib
    /usr/local/lib)
#// 注意cmake调用nvcc进行编译
CUDA_ADD_EXECUTABLE( ${PROJECT_NAME}
    main.cu
)
#// 链接外部库,需要包含cudalib
TARGET_LINK_LIBRARIES( ${PROJECT_NAME}
    ${CUDA_cudadevrt_LIBRARY}
)

相关/参考链接

https://www.jianshu.com/p/5128accb79e2

你可能感兴趣的:(Tool)