6

I create a static library, which contains some CUDA code and some regular C++ code. The CMakeLists.txt for this static library looks like this:

SET(TARGET_H some_header.h)
SET(CUDA_SRC cudaclass1.cu cudaclass2.cu)
SET(TARGET_SRC cppclass1.cpp cppclass2.cpp)
SET(CUDA_NVCC_FLAGS "")
SET(CUDA_SEPARABLE_COMPILATION ON)
CUDA_ADD_LIBRARY(somestatic ${TARGET_H} ${TARGET_SRC} ${CUDA_SRC} OPTIONS -arch sm_20)

This will produce libsomestatic.a.

Now I want to link this static library against an executable, which itself consists of both CUDA code and C++ code.

In the CUDA code of this executable, I need to instantiate and use CudaClass1 from libsomestatic.a.

This is the CMakeLists.txt file for the executable:

SET(TARGET_H some_header.h)
SET(CUDA_SRC cudafile.cu)
SET(TARGET_SRC main.cpp)
SET(CUDA_NVCC_FLAGS "")
SET(CUDA_SEPARABLE_COMPILATION ON)
CUDA_ADD_EXECUTABLE(some_exe ${TARGET_H} ${TARGET_SRC} ${CUDA_SRC} OPTIONS -arch sm_20)
TARGET_LINK_LIBRARIES(some_exe somestatic)

But I get the following error when building:

nvlink error: Undefined reference to '_ZN10test7CudaClass1C1EPKNS_11intE' in ...

I have two questions:

  1. Is it possible at all to accomplish this? Linking CUDA code against a library which contains CUDA code?
  2. If yes, how would I do this using CMake?

The CUDA documentation states (http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#examples)

"It is possible to do multiple device links within a single host executable, as long as each device link is independent of the other (they cannot share code across device executables)"

This sounds like I cannot do what I want to do, right?

I use CUDA 5.5 and GCC 4.8.

talonmies
  • 70,661
  • 34
  • 192
  • 269
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • Have you found a solution to this problem? – BenC Sep 12 '14 at 11:53
  • This could be worth forwarding to the NVIDIA developer in charge of `FindCUDA.cmake`. – BenC Sep 14 '14 at 10:50
  • 2
    I [reported this](http://public.kitware.com/Bug/view.php?id=15157) on the bugtracker. Wait & see. – BenC Sep 14 '14 at 16:39
  • 1
    In the meantime, I ended up making a custom macro that generates the proper compiler/linker commands. – BenC Sep 15 '14 at 12:56
  • Could you please post it here? – m.s. Sep 15 '14 at 13:05
  • 1
    Sure, but be warned that it's in a very raw state and could greatly be improved (but I don't have the time for that): https://github.com/bchretien/cmake-cuda-static. Note that the CMake issue was just assigned to a NVIDIA developer, so we may have a clean solution soon. – BenC Sep 15 '14 at 13:48
  • Do you suppose you could post / post a link to a [MCVE](https://stackoverflow.com/help/mcve) to play around with? – einpoklum Jan 01 '17 at 12:22
  • @einpoklum please have a look at the code BenC linked to: https://github.com/bchretien/cmake-cuda-static – m.s. Jan 01 '17 at 13:51
  • 2
    I know this is an old question, but is it not basically the same as this: https://stackoverflow.com/q/13683575/681865? -- confirmed as an insoluble bug in design of the first generation CUDA support in CMake – talonmies Jul 08 '20 at 12:06
  • OP, are you still experiencing this issue with recent versions of CMake, and specifically with CMake's recent native support for CUDA? – einpoklum Oct 12 '20 at 09:52

0 Answers0