在CMake中链接CUDA库 [英] Linking of CUDA library in CMake

查看:906
本文介绍了在CMake中链接CUDA库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMake 3.10,将已编译的库链接到CMake中的测试可执行文件时遇到问题. 我进行了大量搜索,发现在早期版本中存在一个问题,您无法在结果可执行文件中链接中间库.我不知道这是否已解决还是问题.

I am using CMake 3.10 and have a problem linking a compiled library to a test executable in CMake. I searched a lot and found that in earlier versions there was a problem where you could not link intermediate libraries in the result executable. I was not able to tell if this was resolved or still an issue.

我的CMake文件如下:

My CMake files look like this:

算法:

cmake_minimum_required (VERSION 3.9)
project(${MODULE_NAME}_core LANGUAGES CXX CUDA)


add_subdirectory("${core_impl_dir}" implementation)


set(cuda_src "parallel/ParallelComputation.cu")
set(cuda_hdr "parallel/ParallelComputation.h")

add_library(${PROJECT_NAME} STATIC "${cuda_src}" "${cuda_hdr}"
)


target_include_directories (${PROJECT_NAME} PUBLIC "include/" 
"parallel/"
)

source_group("parallel"  FILES "${cuda_src}" "${cuda_hdr}")


set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})

测试:

project(${MODULE_NAME}_gtest LANGUAGES CXX CUDA)

add_subdirectory("${gtest_impl_dir}" implementation)

add_executable(${PROJECT_NAME} "${gtest_impl_src}")
target_link_libraries(${PROJECT_NAME} ${MODULE_NAME}_core)

enable_testing()
find_package(GTest REQUIRED)
include_directories("${GTEST_INCLUDE_DIRS}")


target_link_libraries(${PROJECT_NAME} ${GTEST_BOTH_LIBRARIES})

source_group("Implementation\\Source Files" FILES "${gtest_impl_src}" )

set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})

add_test(${PROJECT_NAME} ${PROJECT_NAME})

仅构建Algo可以正常工作,但是在构建Test时,例如出现链接错误

Building just Algo works fine, but when also building Test, I get linking errors, for example

../implementation/libmatrix1_testCuda_core.a(ParallelComputation.cu.o):在Funktion中'cudaError cudaMalloc(float **,unsigned long)': tmpxft_00005ad0_00000000-5_ParallelComputation.cudafe1.cpp :(.text + 0x4f2):未定义引用'cudaMalloc'

../implementation/libmatrix1_testCuda_core.a(ParallelComputation.cu.o): In Funktion 'cudaError cudaMalloc(float**, unsigned long)': tmpxft_00005ad0_00000000-5_ParallelComputation.cudafe1.cpp:(.text+0x4f2): Undefined reference 'cudaMalloc'

编辑 使用make VERBOSE=1,我得到了以下链接命令:

EDIT using make VERBOSE=1 I got this linking command:

/usr/bin/c ++ -Wl,-不需要-pthread -g -std = c ++ 14 -Wall
CMakeFiles/matrix1_testCuda_gtest.dir//tests/eclipseProject/algos/testCuda/test/src/main.cpp.o CMakeFiles/matrix1_testCuda_gtest.dir/cmake_device_link.o -o matrix1_testCuda_gtest ../implementation/libmatrix1_testCuda_core.a /usr/lib/libgtest.a/usr/lib/libgtest_main.a

/usr/bin/c++ -Wl,--no-as-needed -pthread -g -std=c++14 -Wall
CMakeFiles/matrix1_testCuda_gtest.dir//tests/eclipseProject/algos/testCuda/test/src/main.cpp.o CMakeFiles/matrix1_testCuda_gtest.dir/cmake_device_link.o -o matrix1_testCuda_gtest ../implementation/libmatrix1_testCuda_core.a /usr/lib/libgtest.a /usr/lib/libgtest_main.a

推荐答案

我通过致电

find_package(CUDA 9.0 REQUIRED)

在两个CMake文件中

. 另外,在Algo文件(包含设备代码)中,我不得不做

in both CMake files. Also, in the Algo file (which contains the device code), I had to do

target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})

我期望对CUDA的语言支持将使这些步骤变得不必要,但显然不是.

I was expecting that the language support for CUDA would make those steps unnecessary, but apparently not.

这篇关于在CMake中链接CUDA库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆