Using Cuda Object Linking with Cmake

c++, cmake, cuda

Solution

How about linking`${CUDA_LIBRARIES}` to whatever targets using `.cu files?

For example in your src directory you could try:

cuda_add_executable(TextureMain TextureMain.cu)
target_link_libraries(TextureMain TextureFunc ${CUDA_LIBRARIES})
install(TARGETS TextureMain DESTINATION bin)

Problem

Right now I am working on a project that uses the object linking capabilities of Cuda 5. Since the project is starting to get complex, I wanted to switch to using cmake to compile the code. However I can't seem to get object linking to work correctly for me. I ended up creating a toy version of the project which gets the same kind of errors as the original project. The toy project consists of a main file (TextureMain.cu) that calls a kernel function to run on the GPU. In each GPU thread an instance of a user-defined class (TextureFunc) is referenced, where the class exists in a separate folder from the main file. The class consists of a TextureFunc.cu and TextureFunc.h file in that folder. Here are the CMakeList.txt files I am using: In the project directory (contains src directory): ``` project(TextureMain) cmake_minimum_required(VERSION 2.8) find_package(CUDA REQUIRED) #------------------------------------------------------------------------------- set(CUDA_NVCC_FLAGS "-arch=compute_20; -code=sm_20; -rdc=true; -lcudadevrt") include_directories(src/TextureFunc) #------------------------------------------------------------------------------- add_subdirectory(src/TextureFunc) add_subdirectory(src) ``` In the src directory (contains TextureMain.cu and TextureFunc directory): ``` cuda_add_executable(TextureMain TextureMain.cu) target_link_libraries(TextureMain TextureFunc) install(TARGETS TextureMain DESTINATION bin) ``` In TextureFunc directory (contains TextureFunc.h and TextureFunc.cu): ``` cuda_add_library(TextureFunc TextureFunc.cu ) target_link_libraries(TextureFunc) ``` When I try to compile this code using the above CMakeList.txt files, I get the following error. ``` Linking CXX executable TextureMain CMakeFiles/TextureMain.dir/./TextureMain_generated_TextureMain.cu.o: In function `__sti____cudaRegisterAll_46_tmpxft_00004c15_00000000_6_TextureMain_cpp1_ii_texRef': /tmp/tmpxft_00004c15_00000000-3_TextureMain.cudafe1.stub.c:2: undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_00004c15_00000000_6_TextureMain_cpp1_ii_texRef' TextureFunc/libTextureFunc.a(TextureFunc_generated_TextureFunc.cu.o): In function `__sti____cudaRegisterAll_46_tmpxft_00004bd8_00000000_6_TextureFunc_cpp1_ii_421ca072': /tmp/tmpxft_00004bd8_00000000-3_TextureFunc.cudafe1.stub.c:8: undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_00004bd8_00000000_6_TextureFunc_cpp1_ii_421ca072' collect2: ld returned 1 exit status make[2]: *** [src/TextureMain] Error 1 make[1]: *** [src/CMakeFiles/TextureMain.dir/all] Error 2 ``` This is obviously a linking error, and it probably has to do with the way that I compile the code using cmake. I think the flags for nvcc are right since I was able to compile this project using a Makefile with the same flags. However, I'm not really sure what else I could be doing wrong. I did notice that the error message references some non-existent .cpp files, but I don't know what to do with that. Any advice that can be given would be greatly appreciated. I'm using cmake version 2.8.8.

Original source