首页 > 解决方案 > 如何使用 cmake 编译 C++ 文件和 CUDA 文件

问题描述

我知道如何使用 cmake 编译具有 cuda 函数(如 cublas)的 C++ 文件。但是,在我的项目中,有一个我自己编写的内核函数。我看g++编译器不知道怎么处理内核<<<,>>>。你能帮我解决这个问题吗?以下是我用来用 cublas 编译 C++ 文件的 CMakeLists。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
cmake_minimum_required(VERSION 3.0)

project(GPU_LMM)
find_package(GSL REQUIRED)
find_package(BLAS REQUIRED)
find_package(CUDA)
if (CUDA_FOUND)
    message("CUDA found")
else()
    message("CUDA not found, doing something alternatively")
endif()

include_directories(test_cuda PRIVARE
                ${GSL_INCLUDE_DIRS}
                ${BLAS_INCLUDE_DIRS}
                ${CUDA_INCLUDE_DIRS}
                ${CUDA_CUBLAS_DIRS}
                                    ${PROJECT_SOURCE_DIR})

add_executable(GPU_LMM main.cpp aux.cpp)
target_link_libraries( GPU_LMM  PRIVATE
                    ${GSL_LIBRARY}
                    ${BLAS_LIBRARIES}
                    ${CUDA_LIBRARIES}
                    ${CUDA_CUBLAS_LIBRARIES})

我有三个文件要编译如下。

main.cpp aux.cpp aux.hpp

aux.cpp 包含 cuda 内核函数。谢谢大家!

标签: c++cmakecudanvcc

解决方案


If you have cuda kernels that have to be compiled with nvcc, their extension should be .cu, no .cpp (so that should be aux.cu). Then cmake will use the proper compiler for the proper file.

Since cmake 3.10.0, CUDA support is even native, you just need to activate the language.


推荐阅读