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.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.