4

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.

6
  • as always: a MCVE would be helpful
    – m.s.
    Jun 4, 2015 at 11:11
  • Are you sure the quotes around the whole list of options are supposed to be there? Jun 4, 2015 at 11:19
  • @angew yes, the problem is still there also without the quotes. I'm working on a MCVE. I also just found this thread which could be useful since the problem seems to be the same. I'm figuring out which version of cmake they are using.
    – Michele
    Jun 4, 2015 at 11:25
  • can't you upgrade to the latest cmake version and try that?
    – m.s.
    Jun 4, 2015 at 12:12
  • 1
    so the problem is solved? Please post an answer explaining what you did. Jun 4, 2015 at 21:52

1 Answer 1

3

It was a cmake problem resolved by this patch (included since cmake 3.2.0). With it the -fPIC flag is passed also when compiling the intermediate link file.

However another problem arised, since in my configuration I have to pass explicitly the host compiler:

[100%] Building NVCC intermediate link file CMakeFiles/shlibcuda.dir/./shlibcuda_intermediate_link.o
    /usr/local/cuda-6.5/bin/nvcc -Xcompiler -fPIC -O3 -gencode arch=compute_32,code=sm_32 -ccbin /usr/bin/g++ -std=c++11 -m64 -ccbin "/usr/bin/gcc-4.8" -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
nvcc fatal   : redefinition of argument 'compiler-bindir'

Because -ccbin /usr/bin/g++ -std=c++11 and -ccbin "/usr/bin/gcc-4.8" are both present.

This is a known cmake open issue n. 0013674. Discussion is still ongoing but applying the patch attached in that thread (it simply removes the check on "-ccbin" in CUDA_NVCC_FLAGS) the problem was solved.

In the discussion of the issue in the new issue tracker, one suggestion is to use the CUDA_HOST_COMPILER variable instead of -ccbin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.