3

During the build of MatCaffe (Matlab wrapper of Caffe), I face the following error :

[ 96%] Built target convert_mnist_data
[ 96%] Built target convert_mnist_siamese_data
[ 98%] Built target pycaffe
[100%] Building Matlab interface: /home/trunks/Downloads/caffe-master/matlab/+caffe/private/caffe_.mexa64
Building with 'g++'.
Warning: You are using gcc version '4.8.2'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '4.8.2-19ubuntu1)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
/usr/bin/ld: cannot find -lpython2
collect2: error: ld returned 1 exit status

make[2]: *** [../matlab/+caffe/private/caffe_.mexa64] Error 255
make[1]: *** [matlab/CMakeFiles/matlab.dir/all] Error 2
make: *** [all] Error 2

On closer examination I found using following command that the following file is responsible for the above error :

 grep -rnw "./" -e "-lpython2"

It revealed to me the following :

./matlab/CMakeFiles/matlab.dir/build.make:53:   cd /home/trunks/Downloads/caffe-master/build/matlab && /usr/local/MATLAB/R2014a/bin/mex -output /home/trunks/Downloads/caffe-master/matlab/+caffe/private/caffe_.mexa64 /home/trunks/Downloads/caffe-master/matlab/+caffe/private/caffe_.cpp -DCPU_ONLY -DWITH_PYTHON_LAYER -DGTEST_USE_OWN_TR1_TUPLE -I/home/trunks/Downloads/caffe-master/src -I/usr/include -I/home/trunks/Downloads/caffe-master/build/external/glog-install/include -I/home/trunks/Downloads/caffe-master/build/external/gflags-install/include -I/home/trunks/Downloads/caffe-master/build/include -I/usr/local/include/opencv -I/usr/local/include -I/usr/include/python2.7 -I/home/trunks/anaconda/lib/python2.7/site-packages/numpy/core/include -I/home/trunks/Downloads/caffe-master/include -I/home/trunks/Downloads/caffe-master/build -L/home/trunks/Downloads/caffe-master/build/lib -L/usr/lib/x86_64-linux-gnu -L/home/trunks/Downloads/caffe-master/build/external/gflags-install/lib -L/home/trunks/Downloads/caffe-master/build/external/glog-install/lib -L/usr/lib -L/usr/local/lib -L/usr/local/lib -L/usr/local/lib -L/usr/local/lib -lcaffe -lboost_system -lboost_thread -lpthread -lgflags -lglog -lhdf5_hl -lhdf5 -llmdb -lleveldb -lsnappy -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopenblas -lpython2 -lboost_python -lprotobuf

So, I changed the corresponding -lpython2 to -lpython2.7 with the hope of resolving the problem. But to no yield.

I also tried the following :

  1. Delete the CMakeCache.txt and doing the make. But it did not work.
  2. I edited the default CMakeLists.txt file in /cmake-master, to change some default settings. I found that the default python version setting in the CMakeLists.txt in Caffe is 2 .

    //Specify which python version to use python_version:STRING=2.7

I changed it to 2 , and repeated the whole configure-generate-make process in a fresh build folder. But to no yield. Everytime the same matlab/build.make file shows -lpython2 , and changing that to 2.7 directly does not yield.

  1. I tried to look into matlab/build.make file but could not find anything over there which I could directly connect with this error.

Any solid help would be deeply appreciated. I use MATLAB 2014a, on Ubuntu 14.04.

Shai
  • 111,146
  • 38
  • 238
  • 371
Ujjwal Aryan
  • 3,827
  • 3
  • 20
  • 31

5 Answers5

4

This is because of a bug in caffe_parse_linker_libs function in Utils.cmake which converts something like /usr/lib/x86_64-linux-gnu/libpython2.7.so to -lpython2

This can be fixed by replacing (in cmake/Utils.cmake)

elseif(IS_ABSOLUTE ${lib})
  get_filename_component(name_we ${lib} NAME_WE)
  get_filename_component(folder  ${lib} PATH)

  string(REGEX MATCH "^lib(.*)" __match ${name_we})
  list(APPEND libflags -l${CMAKE_MATCH_1})
  list(APPEND folders    ${folder})
else()

with

elseif(IS_ABSOLUTE ${lib})
  get_filename_component(folder  ${lib} PATH)
  get_filename_component(filename ${lib} NAME)
  string(REGEX REPLACE "\\.[^.]*$" "" filename_without_shortest_ext ${filename})

  string(REGEX MATCH "^lib(.*)" __match ${filename_without_shortest_ext})
  list(APPEND libflags -l${CMAKE_MATCH_1})
  list(APPEND folders    ${folder})
else()

The updated function correctly converts something like /usr/lib/x86_64-linux-gnu/libpython2.7.so to -lpython2.7

iNFINITEi
  • 1,504
  • 16
  • 23
0

According to error message and command, causes it, it seems that python libary is installed at unusual location, so ld(linker) cannot find it in its default paths. As CMake script has found headers, it should also setup mex executable for work with library itself, but for some reason it doesn't.

The simplest way to make building package work is to set LD_LIBRARY_PATH to the directory, where you python library is located, and run make. If you want to fix CMake script, this wiki may help you.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
0

Thanks to @Tsyvarev for the answer. I found a rather simple solution. I just made a symbolic link (libpython2.so) which points to libpython2.7.so in /usr/lib folder. This solved the problem. libpython2.7.so was also present in /usr/lib so i dont think it was the issue of an unusual install.

Ujjwal Aryan
  • 3,827
  • 3
  • 20
  • 31
0

I had the same problem. In despair I just removed the -lpython2 from build-matlab/matlab/CMakeFiles/matlab.dir/build.make

It did compile after that, seems that it found whatever it needed regardless.

Mike
  • 1,158
  • 5
  • 22
  • 32
0

I had this problem and it seems that it roots in the unability of Cmake to get different versions of python, such as "libpython.so.1.0". I changed my CMakeCache.txt file to "libpython.so" and the problem solved. It is not only for python, I had this issue with my "cudnn" as well, and this solution fixed that.