Installing OpenCV with python module on CentOS goe

2019-04-15 15:30发布

问题:

when I run this command to install OpenCV with Python module

cmake ../ -DCMAKE_BUILD_TYPE=RELEASE 
-DCMAKE_INSTALL_PREFIX=/usr/local 
-DBUILD_EXAMPLES=ON 
-DBUILD_NEW_PYTHON_SUPPORT=ON 
-DINSTALL_PYTHON_EXAMPLES=ON 
-DPYTHON_EXECUTABLE=/usr/local/bin/python2.7 
-DPYTHON_INCLUDE_DIR=/usr/local/include/python2.7/ 
-DPYTHON_LIBRARY=/usr/local/lib/python2.7/config/libpython2.7.a 
-DPYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python2.7/site-packages/numpy/core/include/ 
-DPYTHON_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages/ 
-DBUILD_PYTHON_SUPPORT=ON

I get this error message.

/usr/bin/ld: /usr/local/lib/python2.7/config/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/python2.7/config/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

I can't understand what is wrong and the error message.

Is there someone who can tell me what is wrong with this?

By the way, My OS is CentOS.

and I use Python2.7.5

回答1:

I answer my own question. hope someone who suffers the same problem find a way out of it in a short time.

1.First of all, just update all pagackages using yum I got several bugs attributed to dependency issues when installing OpenCV.

 sudo yum update --skip-broken

2.Rebuild your Python with "--enable-shared". OpenCV with python module requires the "libpython2.7.so" file to be built correctly. However if you have just built python without this configuration, it's likely that you do not have this file. "libpython2.7.a" is not enough. In my case, when I refer libpython2.7.a to be a python library source it crashed down continuously.

So.. download python 2.7.5 (or something like this), and reconfigure like this.

./configure --enable-shared
make
make install

Now you might get "libpython2.7.so" and "libpython2.7.so.1.0"

3.Build your OpenCV with python module. this is what I coded on installation. I guess this example help you kick your problem out.

cmake ../ -DCMAKE_BUILD_TYPE=RELEASE 
-DCMAKE_INSTALL_PREFIX=/usr/local 
-DBUILD_EXAMPLES=ON 
-DBUILD_NEW_PYTHON_SUPPORT=ON 
-DINSTALL_PYTHON_EXAMPLES=ON 
-DPYTHON_EXECUTABLE=/usr/local/bin/python2.7 
-DPYTHON_INCLUDE_DIR=/usr/local/include/python2.7/ 
-DPYTHON_LIBRARY=/usr/local/lib/libpython2.7.so.1.0 
-DPYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python2.7/site-packages/numpy/core/include/ 
-DPYTHON_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages/ 
-DBUILD_PYTHON_SUPPORT=ON

That's all.