OpenCV Python Linker Error

2019-04-07 23:53发布

I've been trying to install opencv on heroku to no avail. I've tried using heroku-anvil for the installation and am now trying to install opencv via the bash shell. First I installed cmake but then when I try to make opencv I inevitably get the following error:

Linking CXX shared library ../../lib/cv2.so
/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `.rodata.str1.8' 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: ld returned 1 exit status
make[2]: *** [lib/cv2.so] Error 1
make[1]: *** [modules/python/CMakeFiles/opencv_python.dir/all] Error 2
make: *** [all] Error 2

Here are the exact commands that I'm using:

# connect to bash shell

heroku run bash
cd /tmp

# Downloading and Installing cmake:

curl -s http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.Z > cmake.tar.gz
tar zxf cmake.tar.gz
cd cmake-2.8.12.1/
./bootstrap
make
make install

# Download and Install OpenCV

curl -s https://s3.amazonaws.com/nerdglasses/opencv/OpenCV-2.4.2.tar.bz2 > opencv.tar.gz
tar xf opencv.tar.gz
cd OpenCV-2.4.2/
../cmake-2.8.12.1/bin/cmake -DBUILD_SHARED_LIBS=NO -DCMAKE_SHARED_LINKER_FLAGS="-fPIC" -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DWITH_OPENEXR=OFF -DCMAKE_FIND_ROOT_PATH=/app/.heroku -DCMAKE_INSTALL_PREFIX=/app/.heroku .
make # ERROR HERE

Anyone know how to fix this? Or if there is a better way to get opencv installed on heroku for a python web app?

2条回答
对你真心纯属浪费
2楼-- · 2019-04-08 00:44

You need to reinstall python with shared and static way.

Firstly, download Python source file again.

and then..

./configure --enable-shared --enable-static
make
make install

In that way you might solve this problem out.

Here's the link that you might want to know.

Installing OpenCV with python module on CentOS goes wrong

查看更多
我只想做你的唯一
3楼-- · 2019-04-08 00:51

Same problem was faced by me on Linux Mint 17.3 while installing Caffe and OpenCV python linking

I didn't want to reinstall python with --enable-shared option since there were quite a few packages dependent on native python installation.

On digging further found that two version's of python were installed in my system one in path /usr/local/lib/python2.7 [built using python download from official website] and other in path /usr/lib/python2.7 [installed with sudo apt-get install/ package manager]

The installation from official website is not Position Independent Code since enable shared was not provided during configuration of python. [PIC, compiler flag needed to be used].

Even after using -fPIC there was no luck and since python at: /usr/local/lib/python2.7 is not useful for me, decided to remove it with below command

sudo rm -rf /usr/local/lib/python2.7

Voila! It works now. This allowed python to be position independent code since it is based on package installation.

查看更多
登录 后发表回答