我正在使用CMake编译一个CUDA项目,该项目包含一个静态库和一个主文件。 MWE here。目录是:
├── CMakeLists.txt
├── src
├── mylib.h
├── mylib.cu
├── test
├── CMakeLists.txt
├── main.cpp
在Ubuntu上一切正常。但是在Windows上我遇到了链接错误:
mylib.lib(mylib.cu.obj) : error LNK2019: unresolved external symbol __cudaRegisterLinkedBinary_40_tmpxft_00006024_00000000_7_mylib_cpp1_ii_935b38c5 referenced in function "void __cdecl __sti____cudaRegisterAll(void)" (?__sti____cudaRegisterAll@@YAXXZ)\build\test\Release\main.exe : fatal error LNK1120: 1 unresolved externals
此问题仅与第一个CMakeLists.txt
:
cmake_minimum_required(VERSION 3.8)
project(MyTest LANGUAGES CXX CUDA)
# check requirements
find_package(CUDA 8.0 REQUIRED)
# set include and link directories
if (UNIX)
set(CUDA_SAMPLE_INC ${CUDA_TOOLKIT_ROOT_DIR}/samples/common/inc)
set(CUDA_TARGET_INC ${CUDA_TOOLKIT_ROOT_DIR}/targets/x86_64-linux/include)
set(CUDA_SAMPLE_LKN ${CUDA_TOOLKIT_ROOT_DIR}/targets/x86_64-linux/lib)
endif (UNIX)
if (WIN32)
set(CUDA_SAMPLE_INC C:/ProgramData/NVIDIA\ Corporation/CUDA\ Samples/v9.0/common/inc)
set(CUDA_TARGET_INC C:/Program\ Files/NVIDIA GPU\ Computing\ Toolkit/CUDA/v9.0/include)
set(CUDA_SAMPLE_LKN C:/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v9.0/lib/x64)
endif (WIN32)
include_directories(src ${CUDA_SAMPLE_INC} ${CUDA_TARGET_INC})
link_directories(${CUDA_SAMPLE_LKN})
# define and compile our static library
set(STATIC_MY_LIB mylib)
add_library(${STATIC_MY_LIB} STATIC src/mylib.cu)
# install
install(TARGETS ${STATIC_MY_LIB}
ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib
LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib)
# comment it out to suppress the error
set_target_properties( ${STATIC_MY_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
# add our test project
add_subdirectory(test)
如果我发表评论set_target_properties( ${STATIC_MY_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
,则链接错误消失。
环境:
为什么会这样?以及如何克服?
答案 0 :(得分:2)
经过几天的努力后,我想找到了解决方案。使用CMake创建静态库时,请设置CUDA_RESOLVE_DEVICE_SYMBOLS,即使用
set_target_properties(your_project PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
根据Nvidia的this文章,此设置强制CMake在构建库时编译并链接所有CUDA符号(函数调用等)。
如果您需要在之前进行可分离的编译设备链接 由共享库或可执行文件使用,您可以显式 通过设置目标属性来请求CMake调用设备链接 CUDA_RESOLVE_DEVICE_SYMBOLS