I'm trying to build a shared library containing CUDA code using CMake. I'm using the package findCUDA. I have a problem in the linking phase:
Linking CXX shared library shlibcuda.so
/usr/bin/c++ -fPIC -std=c++0x -fopenmp -O3 -DNDEBUG -shared -Wl,-soname,shlibcuda.so -o shlibcuda.so CMakeFiles/shlibcuda.dir/src/cuda/./shlibcuda_generated_calibrate.cu.o CMakeFiles/shlibcuda.dir/src/cuda/./shlibcuda_generated_cleaning.cu.o CMakeFiles/shlibcuda.dir/./shlibcuda_intermediate_link.o -L/usr/local/cuda-6.5/lib64/libcudart.so -Wl,-rpath,/mylibs/lib:/usr/local/cuda-6.5/lib64
/usr/bin/ld: CMakeFiles/shlibcuda.dir/./shlibcuda_intermediate_link.o: relocation R_X86_64_32S against `__nv_module_id' can not be used when making a shared object; recompile with -fPIC
CMakeFiles/shlibcuda.dir/./shlibscuda_intermediate_link.o: error adding symbols: Bad value
From this question and its answer I found that maybe the problem could be that one of the object file to be linked was not compiled with the -fPIC
option. I added -Xcompiler -fPIC
to CUDA_NVCC_FLAGS.
In fact, as you can see in the line below, when the build process reaches the building of the so called intermediate link file, no -fPIC
is passed to the compiler:
[100%] Building NVCC intermediate link file CMakeFiles/shlibcuda.dir/./shlibcuda_intermediate_link.o
/usr/local/cuda-6.5/bin/nvcc -m64 -ccbin "/usr/bin/cc" -dlink CMakeFiles/shlibcuda.dir/src/cuda/./shlibcuda_generated_calibrate.cu.o CMakeFiles/shlibcuda.dir/src/cuda/./shlibcuda_generated_cleaning.cu.o -o CMakeFiles/shlibcuda.dir/./shlibcuda_intermediate_link.o
My NVCC flags are the following:
#CUDA include directories
find_package(CUDA REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; -Xcompiler -fPIC; -O3; -gencode arch=compute_32,code=sm_32; -ccbin /usr/bin/g++ -std=c++11)
What am I doing wrong? If the problem is the lack of -fPIC
, how to pass that option when compiling the intermediate link file?
I'm using CUDA 6.5 and I'm passing the -ccbin /usr/bin/g++ -std=c++11
option because I need to use some c++11 in the host code.
My cmake: 2.8.12.2.