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:
- Is it possible at all to accomplish this? Linking CUDA code against a library which contains CUDA code?
- 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.