-1

I am trying to compile a few .cu source files into a python module. Which works fine as long as there is not more than one file. The CMakeLists.txt looks like this:

find_package(CUDA)
find_package(PythonLibs 3.7 REQUIRED)
find_package(pybind11)

file(GLOB_RECURSE sources ${SRC}/launcher.cu)

cuda_add_library(main SHARED ${SRC}/module.cpp ${sources})
target_link_libraries(main ${PYTHON_LIBRARIES} cudart)
set_target_properties(main PROPERTIES CUDA_SEPERABLE_COMPILATION ON)

The above file will compile a python module neatly

But when ${sources} contains several .cu file, the linking just doesn't happen. I receive the following error:

[ 16%] Building NVCC (Device) object
CMakeFiles/main.dir/src/main_generated_launcher.cu.o ptxas fatal   :
Unresolved extern function '_ZN5StateIdE16WithinBoundariesEii'
CMake Error at main_generated_launcher.cu.o.cmake:279 (message):
 Error generating file
 [...]/DNA_PredatorPrey/build/CMakeFiles/main.dir/src/./main_generated_launcher.cu.o

The "Unresolved extern function points" is a function that the linker is supposed to find in another source file.

When using add_executable(....) instead of cuda_add_library on the exact same group of files, it will compile into a working executable file.

So I'm looking for what cuda_add_library requires in order to link files properly.

Cydouzo
  • 445
  • 5
  • 18
  • I know it is a trivial check, but I think it is worth asking. In your not working example with multiple files, have you specified the files directly in the `cuda_add_library` or were you populating a variable with filenames with some `GLOB_RECURSE` and then you used this one inside the `cuda_add_library` call? I'm just trying to cancel any chance it is a problem not related to cuda management (e.g. a missing file or a wrong filepath) – albestro Dec 17 '19 at 19:19
  • @albestro I tried writing the filenames inside the cuda_add_library to no avail – Cydouzo Dec 18 '19 at 02:06
  • Can you try specifying files before the `SHARED` keyword inside the `cuda_add_library` call? e.g `cuda_add_library(main file1 file2 SHARED)` – albestro Dec 18 '19 at 03:03

2 Answers2

2

Linking to cuda libraries this way is not necessary anymore. You can probably use the enable_language(CUDA), which will take care of the linking.

You can have a look at this tutorial or this example from Nvidia dev blogs

Thombou
  • 367
  • 2
  • 15
  • Can you be more specific when you say "Linking to cuda libraries this way is not necessary anymore"? I had already enabled the CUDA language with this command: project(ProjectName LANGUAGES CUDA CXX) – Cydouzo Dec 13 '19 at 02:56
  • You say it is not necessary, but there's probably something necessary that I am not doing. Thank you for the tutorial and the example, I read both and I really don't see what I am missing – Cydouzo Dec 13 '19 at 02:58
2

Since you mentioned you are using directly LANGUAGES CUDA. You may just use add_library() instead of add_cuda_library() as it is supported by cmake as an first languague. I guess some project using older version cmake(<3.8?) would need something like CUDA_ADD_LIBRARY CUDA_ADD_EXECUTABLE. But as a first language, you can just regard it as a normal c project.

Boendal
  • 2,496
  • 1
  • 23
  • 36
b4l8
  • 141
  • 5