我正在尝试将CUDA函数添加到使用CMake的现有C++项目中。
例如,main.cpp
看起来像这样:
#include <stdio.h>
#include "kernels/test.cuh"
int main() {
wrap_test_print();
return 0;
}
而 kernels/test.cu
看起来像这样:
#include "test.cuh"
__global__ void test_print(){
printf("Hello World!\n");
}
void wrap_test_print() {
test_print<<<1, 1>>>();
return;
}
而 kernels/test.cuh
看起来是这样的:
#ifndef TEST_CUH__
#define TEST_CUH__
#include <stdio.h>
void wrap_test_print();
#endif
我使用以下代码来编写 CMakeLists.txt
:
===============
CMakeLists.txt
===============
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
enable_language(CUDA)
project(cmake_and_cuda)
add_executable(main main.cpp)
add_subdirectory(kernels)
# set_property(TARGET main
# PROPERTY CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(main kernels)
===============
kernels/CMakeLists.txt
===============
enable_language(CUDA)
add_library(kernels
test.cu
test.cuh
)
target_compile_features(kernels PUBLIC cxx_std_11)
set_target_properties(kernels
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(kernels)
然而,当我在项目的build
文件夹中使用cmake ..
命令时,将会打印以下错误消息:
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_CUDA_DEVICE_LINK_LIBRARY
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_CUDA_DEVICE_LINK_LIBRARY
我猜这是由问题引起的,所以我添加了set(CMAKE_CXX_FLAGS”$ {CMAKE_CXX_FLAGS} -lcudart”)
,但我无法解决这个问题。我该如何解决这个问题?