在构建 MatCaffe(Caffe 的 Matlab 包装器)期间,我遇到以下错误:

[ 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

经过仔细检查,我发现使用以下命令发现以下文件是造成上述错误的原因:
 grep -rnw "./" -e "-lpython2"

它向我揭示了以下内容:
./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

所以,我把对应的-lpython2改成了-lpython2.7,希望能解决问题。但没有收获。

我还尝试了以下方法:
  • 删除 CMakeCache.txt 并进行 make。但它没有用。
  • 我编辑了/cmake-master 中的默认 CMakeLists.txt 文件,以更改一些默认设置。我发现 Caffe 中 CMakeLists.txt 中的默认 python 版本设置是 2 。

    //指定使用哪个python版本
    python_version:STRING=2.7

  • 我将其更改为 2 ,并在新的构建文件夹中重复整个配置-生成-制作过程。但没有收获。每次相同的 matlab/build.make 文件都显示 -lpython2 ,并将其直接更改为 2.7 不会产生。
  • 我试图查看 matlab/build.make 文件,但在那里找不到任何可以直接与此错误联系的内容。

  • 任何可靠的帮助将不胜感激。我在 Ubuntu 14.04 上使用 MATLAB 2014a。

    最佳答案

    这是因为 Utils.cmake 中的 caffe_parse_linker_libs 函数中存在一个错误,它将类似 /usr/lib/x86_64-linux-gnu/libpython2.7.so 的内容转换为 -lpython2
    这可以通过 替换 来解决(在 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()
    


    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()
    

    更新后的函数正确地将类似 /usr/lib/x86_64-linux-gnu/libpython2.7.so 的内容转换为 -lpython2.7

    关于python - 找不到 -lpython2 : MatCaffe installation error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31365592/

    10-11 17:00