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.

Community
  • 1
  • 1
Michele
  • 2,796
  • 2
  • 21
  • 29
  • as always: a [MCVE](http://stackoverflow.com/help/mcve) would be helpful – m.s. Jun 04 '15 at 11:11
  • Are you sure the quotes around the whole list of options are supposed to be there? – Angew is no longer proud of SO Jun 04 '15 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](http://public.kitware.com/pipermail/cmake/2015-January/059489.html) 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 04 '15 at 11:25
  • can't you upgrade to the latest cmake version and try that? – m.s. Jun 04 '15 at 12:12
  • @m.s. done. Another problem arised, but fixed with another patch. Now I'll wait for the FindCUDA.cmake improved code to be released. Thanks. – Michele Jun 04 '15 at 15:06
  • 1
    so the problem is solved? Please post an answer explaining what you did. – Robert Crovella Jun 04 '15 at 21:52

1 Answers1

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.

Michele
  • 2,796
  • 2
  • 21
  • 29