7

I am building YouCompleteMe plugin of vim, following this document. When I run make I get the following error.

Linking CXX shared library /home/sagar/.vim/bundle/YouCompleteMe/python/ycm_core.so
/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpython2.7.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status

What is this error?
I have installed pyenv to manage python versions. Is it causing problem?

Sagar Rakshe
  • 2,682
  • 1
  • 20
  • 25

2 Answers2

7

Make the linker point to the .so (shared object) file and not the .a (static lib) file.

You can do this specifying the flag when running cmake:

cmake -G "Unix Makefiles" -DPYTHON_LIBRARY=/usr/local/lib/libpython2.7.so . ~/.vim/bundle/YouCompleteme/cpp

Do mind that even though you're using pyenv, YouCompleteMe build may point to an undesired python build as they are not correctly auto-detected right now.

If you're having this problem, you should probably also specify the Python header files correctly:

cmake -G "Unix Makefiles" -DPYTHON_LIBRARY=/usr/local/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR=/usr/local/include/python . ~/.vim/bundle/YouCompleteme/cpp

PS=(I'm assuming your headers are in that path, do check before)

Felipe Lema
  • 2,700
  • 12
  • 19
6

Since some paths were different on my system from the accepted answer (both the CMake and the python lib ones) I'm posting an alternate solution for the above problem:

  1. Make sure to have a shared library version of libpython2.7.so

    $ locate libpython
    /usr/lib/x86_64-linux-gnu/libpython2.7.so.1
    
  2. Either create a symlink to it from where CMake expects it to be

    sudo ln -s "/usr/lib/x86_64-linux-gnu/libpython2.7.so.1" "/usr/lib/libpython2.7.so"
    

    or alternatively, as written in YCM's build script code, you could add additional CMake options to ensure the .so library is properly found

    export EXTRA_CMAKE_ARGS="-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so.1"
    
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 1
    Had the same error trying to compile YCM, symlinking the shared library didn't work for me, but exporting CMake options did, thanks :-) – Eddy Jan 18 '16 at 09:14
  • 1
    I had this error while compiling Caffe on my Ubuntu server. Creating a symbolic link solved my problem. Thanks! – BajajG Jan 20 '17 at 11:30