c++ - 使用 CMake 编译 CUDA 时出现错误 "undefined reference to ` __cudaRegisterLinkedBinary"

标签 c++ cuda cmake

我正在尝试使用 CMake 编译一个使用 C++ 和 CUDA 的程序。 编译 c++ 文件的 CMakeLists 部分已经完成,我正在添加编译 CUDA 文件的部分。 我添加了源代码并且它们编译正常,但是当链接部分出现时它停止了。 这是 CMakeLists.txt 文件:

cmake_minimum_required (VERSION 2.6)

set (CMAKE_CXX_STANDARD 11)
set(PROJECT_NAME "UFFS - Empty Project" CACHE STRING "Nome do Projeto")

project (${PROJECT_NAME})

find_package(OpenGL REQUIRED)   
find_package(CUDA REQUIRED)
add_subdirectory (external)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-1c73e35")
include(CreateLaunchers)
include(MSVCMultipleProcessCompile)

if(INCLUDE_DISTRIB)
    add_subdirectory(distrib)
endif(INCLUDE_DISTRIB)

set(CUDA_NVCC_FLAGS -Wno-deprecated-gpu-targets;-rdc=true;-std=c++11)

include_directories(
    external/glfw-2.7.6/include
    external/glm-0.9.4.0
    external/glew-1.9.0/include
    external/RVO2/include
    external/GoTools/gotools-core/include
    external/GoTools/parametrization/include
    external/SFML/include
    external/OpenNL/include
    CUDA/include
    .
)

add_library(ant_teak_bar STATIC IMPORTED)
set_property(TARGET ant_teak_bar PROPERTY IMPORTED_LOCATION external/AntTweakBar/lib/libAntTweakBar.a)

add_library(openNL STATIC IMPORTED)
set_property(TARGET openNL PROPERTY IMPORTED_LOCATION external/OpenNL/lib/openNL.a)

set(ALL_LIBS
    ${OPENGL_LIBRARY}
    ${CUDA_LIBRARY}
    GLFW_276
    GLEW_190
    RVO
    parametrization
    pthread
    sfml-audio
    sfml-window
    ant_teak_bar
    openNL
)

add_definitions(
    -DTW_STATIC
    -DTW_NO_LIB_PRAGMA
    -DTW_NO_DIRECT3D
    -DGLEW_STATIC
    -D_CRT_SECURE_NO_WARNINGS
)

file(GLOB main_src
    "*.h"
        "*.hpp"
    "*.cpp"
        "CUDA/include/CUDA/*.cuh"
        "OpenCL/*.cpp"
        "common/*.cpp"
        "common/*.hpp"
        "common/*.h"
)


cuda_compile(   
    AGENT_O CUDA/_Agent.cu
)
cuda_compile(   
    GEOMETRIC_O CUDA/cudaGeometric.cu
)
cuda_compile(   
    KERNEL_O    CUDA/kernel.cu
)
cuda_compile(   
    RVOAGENT_O  CUDA/RVOAgent.cu
)
cuda_compile(   
    RVOSIMULATOR_O  CUDA/RVOSimulator.cu
)

cuda_add_executable(main ${main_src}
    ${DLINK_O}
    ${AGENT_O}
    ${GEOMETRIC_O}
    ${KERNEL_O}
    ${RVOAGENT_O}
    ${RVOSIMULATOR_O}
    ${CUDA_LIBRARY}
    )




set_target_properties(main PROPERTIES XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/")
create_target_launcher(main WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/")
create_default_target_launcher(main WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/")

target_link_libraries(main ${ALL_LIBS})


SOURCE_GROUP(common REGULAR_EXPRESSION ".*/common/.*" )

if (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )
add_custom_command(
   TARGET main POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/main${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/"
)

elseif (${CMAKE_GENERATOR} MATCHES "Xcode" )

endif (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )

这是 cmake 的输出:

-- Could NOT find Boost
failed to create symbolic link '/home/fcg/Desktop/TCC_CUDA_LINUX/TCC_OPENCL/external/GoTools/lrsplines2D/data' because existing path cannot be removed: Is a directory
CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:196 (string):
  Policy CMP0053 is not set: Simplify variable reference and escape sequence
  evaluation.  Run "cmake --help-policy CMP0053" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  For input:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  the old evaluation rules produce:

    'LD_LIBRARY_PATH=:$LD_LIBRARY_PATH@LAUNCHER_LINESEP@LD_LIBRARY_PATH=:$LD_LIBRARY_PATH'

  but the new evaluation rules produce:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  Using the old result for compatibility since the policy is not set.
Call Stack (most recent call first):
  external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:275 (_launcher_process_args)
  CMakeLists.txt:105 (create_target_launcher)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:251 (get_target_property):
  Policy CMP0026 is not set: Disallow use of the LOCATION target property.
  Run "cmake --help-policy CMP0026" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

  The LOCATION property should not be read from target "main".  Use the
  target name directly with add_custom_command, or use the generator
  expression $<TARGET_FILE>, as appropriate.

Call Stack (most recent call first):
  external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:280 (_launcher_create_target_launcher)
  CMakeLists.txt:105 (create_target_launcher)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:196 (string):
  Policy CMP0053 is not set: Simplify variable reference and escape sequence
  evaluation.  Run "cmake --help-policy CMP0053" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  For input:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  the old evaluation rules produce:

    'LD_LIBRARY_PATH=:$LD_LIBRARY_PATH@LAUNCHER_LINESEP@LD_LIBRARY_PATH=:$LD_LIBRARY_PATH'

  but the new evaluation rules produce:

    '@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@'

  Using the old result for compatibility since the policy is not set.
Call Stack (most recent call first):
  external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:265 (_launcher_process_args)
  CMakeLists.txt:106 (create_default_target_launcher)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:251 (get_target_property):
  Policy CMP0026 is not set: Disallow use of the LOCATION target property.
  Run "cmake --help-policy CMP0026" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

  The LOCATION property should not be read from target "main".  Use the
  target name directly with add_custom_command, or use the generator
  expression $<TARGET_FILE>, as appropriate.

Call Stack (most recent call first):
  external/rpavlik-cmake-modules-1c73e35/CreateLaunchers.cmake:270 (_launcher_create_target_launcher)
  CMakeLists.txt:106 (create_default_target_launcher)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/fcg/Desktop/TCC_CUDA_LINUX/TCC_OPENCL

下面是出现错误的“make”输出部分:

[ 14%] Linking CXX executable main
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated__Agent.cu.o: In function `__sti____cudaRegisterAll_41_tmpxft_0000156c_00000000_7__Agent_cpp1_ii_59b6e43a()':
/tmp/tmpxft_0000156c_00000000-4__Agent.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_0000156c_00000000_7__Agent_cpp1_ii_59b6e43a'
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_cudaGeometric.cu.o: In function `__sti____cudaRegisterAll_48_tmpxft_000015a3_00000000_7_cudaGeometric_cpp1_ii_f05e008c()':
/tmp/tmpxft_000015a3_00000000-4_cudaGeometric.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_48_tmpxft_000015a3_00000000_7_cudaGeometric_cpp1_ii_f05e008c'
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_kernel.cu.o: In function `__sti____cudaRegisterAll_41_tmpxft_000015da_00000000_7_kernel_cpp1_ii_ac061e5b()':
/tmp/tmpxft_000015da_00000000-4_kernel.cudafe1.stub.c:2: undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_000015da_00000000_7_kernel_cpp1_ii_ac061e5b'
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_RVOAgent.cu.o: In function `__sti____cudaRegisterAll_43_tmpxft_00001611_00000000_7_RVOAgent_cpp1_ii_5e2ad135()':
/tmp/tmpxft_00001611_00000000-4_RVOAgent.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_43_tmpxft_00001611_00000000_7_RVOAgent_cpp1_ii_5e2ad135'
CMakeFiles/cuda_compile.dir/CUDA/cuda_compile_generated_RVOSimulator.cu.o: In function `__sti____cudaRegisterAll_47_tmpxft_00001535_00000000_7_RVOSimulator_cpp1_ii_a19d5d58()':
/tmp/tmpxft_00001535_00000000-4_RVOSimulator.cudafe1.stub.c:11: undefined reference to `__cudaRegisterLinkedBinary_47_tmpxft_00001535_00000000_7_RVOSimulator_cpp1_ii_a19d5d58'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:2235: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:71: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2

我查了一下,显然当我使用 -rdc=true 标志编译 CUDA 程序时,我需要做一个额外的步骤来正确链接 cuda 库,但我不知道如何在 cmake 中做到这一点,我已经尝试将“${CUDA_LIBRARY}”添加到与“target_link_libraries(main ${ALL_LIBS})”链接的“${ALL_LIBS}”,但这没有用。

最佳答案

如果你看https://cmake.org/cmake/help/v3.0/module/FindCUDA.html , 你会发现你的来电

find_package(CUDA REQUIRED)

定义 CUDA_LIBRARIES,而不是 CUDA_LIBRARY。 我想改变这个会解决你的问题。

关于c++ - 使用 CMake 编译 CUDA 时出现错误 "undefined reference to ` __cudaRegisterLinkedBinary",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385346/

相关文章:

c++ - 内核函数参数为 const

cmake - 如何修复明显损坏的 CMake 构建?

python - 如何为Python安装face_recognition模块

2 个移位图像与 OpenCV 的 C++ 互相关

C++动画光标

memory - cuda 'memory bound' vs 'latency bound' vs 'bandwidth bound' vs 'compute bound'

c++ - 为什么 CMakeLists 文件需要位于 CLion 项目文件夹的根目录中?

函数复制中的 C++ 双重错误

c++ - 在 Visual C++ 中使用类模板

c - 与 Geforce 相比,Tesla 的内核开销要少多少?