Question

Building my project using CMake I get the following linker error and don't know how to solve it. Using make VERBOSE=1 reveals the generated commands which look ok to me:

azg@HPC:~/code/myproject/build_debug$ /usr/bin/c++ -Wall -Wextra -Wshadow -Woverloaded
   -virtual  -g3 -ggdb3 -fopenmp CMakeFiles/mymain.dir/main/cpp
   /algorithm/mymain.cc.o -o mymain -rdynamic 
   my_library.a /opt/dev/boost/lib/libboost_filesystem.a /opt/dev/boost      
   /lib/libboost_graph.a /opt/dev/boost/lib/libboost_system.a /opt/dev/boost
   /lib/libboost_program_options.a /opt/dev/boost/lib/libboost_chrono.a /usr/local
   /hdf5/lib/libhdf5.so -lz -lrt -ldl -lm /usr/local/cuda-5.0/lib64/libcudart.so -lcuda 
   /usr/local/cuda-5.0/lib64/libcublas.so /opt/dev/magma/1.4.0-beta2/lib/libmagma.a  
   /opt/intel/lib/intel64/libimf.so -Wl,-rpath,/usr/local/hdf5/lib:/usr/local/cuda-
   5.0/lib64:/opt/intel/lib/intel64/usr/bin/ld:/usr/local/cuda-5.0/lib64/libcublas.so: 

and the linker error:

undefined reference to symbol 'cudaStreamCreate'
/usr/bin/ld: note: 'cudaStreamCreate' is defined in DSO /usr/local/cuda-5.0/lib64/libcudart.so so try adding it to the linker command line
/usr/local/cuda-5.0/lib64/libcudart.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
Was it helpful?

Solution

The linker error message is telling you exactly what to do to fix this.

Don't specify shared libraries as files, that passes them to the compiler which ignores them. Pass each library search path to the linker with -L and each library stub name with -l. The linker will search for a suitable version of the library (either shared or static depending on what therer is available and the build options you pass) and then link the full list of dependencies together. The resulting build command might look like:

c++ -Wall -Wextra -Wshadow -Woverloaded \
   -virtual  -g3 -ggdb3 -fopenmp CMakeFiles/mymain.dir/main/cpp \
   /algorithm/mymain.cc.o -o mymain -rdynamic my_library.a \
   -L/opt/dev/boost/lib \
   -L/usr/local/hdf5/lib \
   -L/usr/local/cuda-5.0/lib64 \
   -L/opt/dev/magma/1.4.0-beta2/lib \
   -L/opt/intel/lib/intel64 \
   -lboost_filesystem \
   -lboost_graph \
   -lboost_system \
   -lboost_program_options \
   -lboost_chrono \
   -lhdf5 \
   -lz -lrt -ldl -lm \
   -lcudart \
   -lcuda \
   -lcublas \
   -lmagma \
   -limf
   -Wl,-rpath,/usr/local/hdf5/lib:/usr/local/cuda- 5.0/lib64:/opt/intel/lib/intel64/usr/bin/ld

[huge disclaimer: cut and pasted on an ipad on the end of a very flaky GSM connection in the middle of lapland. Not expected to actually work]

I have no idea how you get Cmake to do that, and you haven't shown us a Makefile so that is about as much help as I can offer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top