Ubuntu14.04 64位系统下安装python3.4对应的opencv3.0报错recompile with -fPIC 的解决办法

之前的Ubuntu14.04 64位系统下安装了ffmpeg,后来由于需要使用安装在python3.4环境下可以使用的python视觉库opencv3.0,下面是安装过程中遇到的一些问题。

首先。

1、对于windows7 64位下安装opencv3.0可以参考本人的这篇文章:Windows7 64位+python3.4环境下安装opencv3.0的方法

2、对于Ubuntu 14.04 64位环境下安装opencv3.0可以参考这篇文章:Ubuntu14.04 64位+Python3.4环境下安装opencv3.0的方法

这里之所以再次提出为什么要在Ubuntu环境下安装opencv3.0呢?似乎看上去与“2”重复了啊,其实不是,由于各种实际项目开发问题,往往我们在安装opencv3.0的时候,环境已经不是如“2”中所说的纯净环境了,比如已经安装了ffmpeg,这时候会报如下错误:

[ 38%] Building CXX objectmodules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o

Linking CXX shared library../../lib/libopencv_videoio.so

/usr/bin/ld: cannot find -lavcodec

/usr/bin/ld:/usr/local/lib/libavutil.a(log.o): relocation R_X86_64_32 against`.rodata.str1.1' can not be used when making a shared object; recompile with-fPIC

/usr/local/lib/libavutil.a: error addingsymbols: Bad value

collect2: error: ld returned 1 exit status

make[2]: ***[lib/libopencv_videoio.so.3.0.0] Error 1

make[1]: ***[modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2

make: *** [all] Error 2


因此,我们的目的就是如何在环境中已经安装ffmpeg的基础上能够成功安装opencv3.0。

我的解决过程如下:

步骤1:由于我的环境中ffmpeg是通过下载源码后自己编译生成,因此先进入到ffmpeg的目录,然后执行如下命令来卸载ffmpeg

sudo make uninstall

步骤2:执行如下脚本

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. sudo apt-get update  
  3. sudo apt-get install python3-setuptools python3-dev -y  
  4. sudo easy_install3 pip  
  5. pip3 install numpy  
  6.   
  7. sudo apt-get install build-essential -y  
  8. sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -y  
  9.   
  10. cd ~  
  11. wget https://github.com/Itseez/opencv/archive/3.0.0.zip  
  12. #注意这里不是 cd 3.0.0  , 而是 cd opencv-3.0.0  
  13. unzip 3.0.0.zip && cd opencv-3.0.0  
  14. mkdir build && cd build  
  15.   
  16. cmake -D CMAKE_BUILD_TYPE=Release \  
  17. -D CMAKE_INSTALL_PREFIX=/usr/local  \  
  18. PYTHON3_EXECUTABLE=/usr/bin/python3 \  
  19. PYTHON_INCLUDE_DIR=/usr/include/python3.4 \  
  20. PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m.so \  
  21. PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/dist-packages/numpy/core/include ..  
  22.   
  23. # make -j [N] :表示在那个一时间内进行编译的任务数 ,如果-j 后面不跟任何数字,则不限制处理器并行编译的任务数,现在发现直接用make -j4 安装会失败  
  24. #所以还是用 make -j1 成功了  
  25. make -j1  
  26. sudo make install  
成功。

注意:一定要记得在执行完 上述脚本中第25行的 "make -j1" 后,要记得执行 "sudo make install"。

验证安装成功的方法是:

执行如下命令进入python3交互环境

python3

执行如下命令

import cv2

如果没有输出,表明opencv3.0安装成功



这里附上另一个解决办法给大家参考,希望给大家一些启示。但是对于我而言,并没有成功。

原文地址:http://stackoverflow.com/questions/28960944/opencv-installation-error-ubuntu


I'm trying to install opencv to my Ubuntu 14.04 machine using the documentation provided in

http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation

However, during the compilation process I get the following error.

Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libopencv_videoio.so.3.0.0] Error 1
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2

I noticed a few posts mentioning similar errors and some solutions suggest removing the libavcodec.a from /usr/local/lib/. I'd like not to do that as I need ffmpeg libraries in the machine as well.

Could someone please help me to resolve this issue?.

Thanks

shareimprove this question
 
 
Did you try the suggestion in the error message 'recompile with -fPIC' ? – Ha Dang Mar 10 '15 at 10:15
 
Yes. I tried editing the cmake list file by adding set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" ). But that didn't work. I also tried to recompile ffmpeg with --enable-shared. But that didn't work as well. – userDtrm Mar 10 '15 at 11:48
 
Did you try to remove the libavcodec.a from the folder /usr/local/lib then rerun the compilation? You can move libavcodec.a to somewhere else then put it back to /usr/local/lib once you've done with opencv compilation. – Ha Dang Mar 10 '15 at 12:08
 
I had to remove libavformat.a libavutil.a libavcodec.a libswscale.a from /usr/local/lib and then compile opencv. Then the removed files were copied back to the location. It seems to have worked. Thanks a lot for the help. – userDtrm Mar 10 '15 at 18:14 
 
Glad that it works. Have fun OpenCV-ing! :-) – Ha Dang Mar 10 '15 at 21:36

阅读更多
文章标签: python opencv
个人分类: python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Ubuntu14.04 64位系统下安装python3.4对应的opencv3.0报错recompile with -fPIC 的解决办法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭