1

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".
Antoine Morrier
  • 3,930
  • 16
  • 37
  • `-rdc=true` is not necessary as it is specified automatically when using [`CUDA_SEPARABLE_COMPILATION`](https://github.com/Kitware/CMake/blob/master/Modules/FindCUDA.cmake#L1380). aside from that, I guess you must separate the entries you want to add to a CMake list by a semicolon. So it would be `list(APPEND CUDA_NVCC_FLAGS "-std=c++11;-rdc=true")`, however, `list(APPEND CUDA_NVCC_FLAGS "-std=c++11")` is sufficient. – m.s. Jun 28 '15 at 20:41
  • When you say I have to use semilicon, is I can't use GLOB_RECURSE? Or it is for glags? If it is, it doesn't work as well without -rdc=true ^_^. Thanks :). – Antoine Morrier Jun 28 '15 at 21:43
  • the semicolon is necessary within `list(APPEND...`). if it does not work for you, you should a) clean the CMake cache by deleting the build directory and configure using CMake once again to make sure that your changes were applied b) if this does not help post a [**Minimal**, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) which contains all necessary files (source etc) to reproduce your problem – m.s. Jun 28 '15 at 21:54
  • For all people with the _cuda_get_important_host_flags error stumbling upon this post. I had the same issue and was able to fix it compiling with "-fPIC". – hhergeth Dec 02 '15 at 02:34

2 Answers2

1

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)
Community
  • 1
  • 1
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 1
    Hello, thanks for answer but I have another issue I don't success to solve... 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) – Antoine Morrier Jun 28 '15 at 13:24
  • Update your question with your current Cmake file. – m.s. Jun 28 '15 at 15:27
  • this is a now deprecated way of using CUDA on the CMake level – DomTomCat Feb 28 '23 at 07:24
0

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.

DomTomCat
  • 8,189
  • 1
  • 49
  • 64