Antoine Morrier
Antoine Morrier

Reputation: 4076

Cuda CMake : undefined reference

I am trying to compile my project with cmake, but it's difficult. My project compile with one simple make, but not with cmake. The error is during the link. CMake prefers launch g++ ... -o ... instead of nvcc ... -o ...

If I force nvcc, the error is -rdynamic is unknown.

So, it's my cmake file

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)

and here errors :

/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() »:
....

EDIT : After answer, I changed my CMakeLists.txt by this one :

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)

And now, I have these errors :

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".

Upvotes: 1

Views: 3405

Answers (2)

DomTomCat
DomTomCat

Reputation: 8569

Modern CMake does not use the find_package(CUDA REQUIRED) kind of style anymore, see also https://cliutils.gitlab.io/modern-cmake/chapters/packages/CUDA.html . CUDA has become a CMake language, just like CXX:

set(PROJECT_NAME MY_PROJECT)
project(${PROJECT_NAME} LANGUAGES CUDA CXX)

You can also test if CUDA is available:

check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
    enable_language(CUDA)

    # to tell your code it was found via #ifdef USE_CUDA:
    add_definitions(-DUSE_CUDA)    

    include_directories("${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}")
endif()

# after definition of executable or library dont forget cuda arch version(s)

set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES "35;50;72")

This includes auto-linking etc.

Upvotes: 0

m.s.
m.s.

Reputation: 16354

The details of the linker error are discussed in this SO question: CUDA Dynamic Parallelism MakeFile

In order to solve it using CMake, you need to set CUDA_SEPARABLE_COMPILATION to ON. I'd also suggest using the latest CMake version (3.x) since there were some bugfixes to FindCUDA since version 2.8.

Your CMakeLists.txt file would then look like this:

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)

Upvotes: 1

Related Questions