Cuda CMake:未定义的参考

时间:2015-06-28 07:02:46

标签: c++ cuda cmake

我正在尝试使用cmake编译我的项目,但这很困难。我的项目用一个简单的make编译,但不用cmake编译。错误发生在链接期间。 CMake更喜欢启动g ++ ... -o ...而不是nvcc ... -o ...

如果我强制使用nvcc,则错误是-rdynamic未知。

所以,这是我的cmake文件

cmake_minimum_required(VERSION 2.8)
project(LightRays)

find_package(CUDA QUIET REQUIRED)

list(APPEND CUDA_NVCC_FLAGS "-std=c++11;-rdc=true")

file(GLOB_RECURSE
        source_file
        src/*
        include/*)


CUDA_ADD_EXECUTABLE(LightRays ${source_file})

target_link_libraries(LightRays -lSDL -L/opt/cuda/lib64 -lcuda -lcudart)

add_definitions(-std=c++11)

target_link_libraries(LightRays -lSDL -L/opt/cuda/lib64 -lcuda -lcudart)

add_definitions(-std=c++11)

这里有错误:

/tmp/tmpxft_00006509_00000000-4_global.cudafe1.stub.c:8: référence indéfinie vers « __cudaRegisterLinkedBinary_41_tmpxft_00006509_00000000_7_global_cpp1_ii_0ad406bb »
CMakeFiles/LightRays.dir/src/tools/LightRays_generated_tools.cu.o: dans la fonction « __sti____cudaRegisterAll_40_tmpxft_00006518_00000000_7_tools_cpp1_ii_278b9139() »:
....

编辑:回答后,我改变了我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(LightRays)

find_package(CUDA REQUIRED)

list(APPEND CUDA_NVCC_FLAGS "-std=c++11 -rdc=true")
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
add_definitions(-std=c++11)

set(CUDA_SEPARABLE_COMPILATION ON)

file(GLOB_RECURSE
    source_file
    src/*
    include/*)

cuda_add_executable(LightRays ${source_file})
target_link_libraries(LightRays -lSDL)

现在,我有这些错误:

CMake Error at /usr/share/cmake-3.2/Modules/FindCUDA.cmake:1455 (_cuda_get_important_host_flags):
  _cuda_get_important_host_flags Function invoked with incorrect arguments
  for function named: _cuda_get_important_host_flags
Call Stack (most recent call first):
  /usr/share/cmake-3.2/Modules/FindCUDA.cmake:1570 (CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS)
  CMakeLists.txt:17 (cuda_add_executable)


-- Configuring incomplete, errors occurred!
See also "/home/qnope/Programmation/cuda/LightRay/build/CMakeFiles/CMakeOutput.log".

1 个答案:

答案 0 :(得分:1)

此SO问题中讨论了链接器错误的详细信息: CUDA Dynamic Parallelism MakeFile

要使用CMake解决此问题,您需要将CUDA_SEPARABLE_COMPILATION设置为ON。 我还建议使用最新的CMake版本(3.x),因为自2.8版以来,FindCUDA有一些错误修正。

您的CMakeLists.txt文件将如下所示:

cmake_minimum_required(VERSION 3.0)
project(LightRays)

find_package(CUDA REQUIRED)

list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
add_definitions(-std=c++11)

set(CUDA_SEPARABLE_COMPILATION ON)

file(GLOB_RECURSE
    source_file
    src/*
    include/*)

cuda_add_executable(LightRays ${source_file})
target_link_libraries(LightRays -lSDL)