linux ubuntu16.04 opencv3.4.2 cuda9 安装编译填坑记

首先说明,在此次填坑之前已经尝试了很多次在同样环境下安装opencv,包括opencv 2.4版本,均告失败。但借此可能装上了一些必要的依赖。所以这次编译的时候,只出现了这些错误。如果是初次在同样的环境下安装,可能还会报别的错误。比如,我这次就没有出现CUDA_nppi_LIBRARY (ADVANCED)的错误,可能是因为是opencv3.4版本比较高的原因,3.2有可能出现那个错误。

编译安装opencv不要用默认安装路径(CMAKE_INSTALL_PREFIX=/usr/local)!!!!!!后续多版本opencv共存时会存在很大问题!!!

完整安装编译过程见文末。

 

==========================================================

注意事项:

过程主要参考:https://blog.csdn.net/weixin_42271913/article/details/88997729

1,cmake ..时每次都要下载ippicv_2017u3_lnx_intel64_general_20180518.tgz

手动下载好这个文件,地址:https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20180518/ippicv/ippicv_2017u3_lnx_intel64_general_20180518.tgz

然后修改/home/xxx/opencv-3.4.2/opencv/3rdparty/ippicv/ippicv.cmake , 将47行的 "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"

改为手动下载的文件的本地路径,如:"file:///home/xxx/cmake_files/"(仅供参考,根据自己的路径填写)

参考:https://blog.csdn.net/u010739369/article/details/79966263

 

2,同上,每次都要下载 face_landmark_model.dat,改一下文件读取本地的,不从网上下载。

将opencv_contrib-3.4.2/modules/face/CMakeLists.txt文件的第19行的"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"修改为本地路径,如"file:///home/xxx/cmake_files/"(仅供参考,根据自己的路径填写),即将原来的网址修改为下载的文件保存的路径

参考:https://blog.csdn.net/CSDN330/article/details/86747867

 

3,同上。将opencv_contrib-3.4.2解压到opencv-3.4.2目录中,然后进行编译安装,在cmake的过程中 /home/user/opencv-3.4.2/opencv_contrib-3.4.2/modules/xfeatures2d/src 中的如下文件可能下载失败:

\boostdesc 

boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
boostdesc_lbgm.i

\vgg 

vgg_generated_120.i
vgg_generated_48.i
vgg_generated_64.i
vgg_generated_80.i

可以修改 /home/user/opencv-3.4.2/opencv_contrib-3.4.2/modules/xfeatures2d/cmake 中两个下载文件(download_boostdesc.cmake,download_vgg.cmake )将下载地址改为本地目录。

这些文件的下载地址:

boostdesc: https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/*, *可以替换为 boostdesc_bgm_hd.i 等。

vgg: https://github.com/opencv/opencv_3rdparty/tree/contrib_xfeatures2d_vgg_20160317 下载zip包。

 

4,如果make -j的过程中报:make[2]: *** No rule to make target `/usr/lib/libnvcuvid.so‘, needed by `lib/libopencv_cudacodec.so.3.1.0‘. Stop.的错误,则重新建立软连接。

参考:http://www.dongcoder.com/detail-730925.html

 

3,cmake命令(运行通过,请按照自己的情况进行设置,文末附完整过程有解释):

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv/3.4.2 -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=/home/xxx/Downloads/opencv_contrib-3.4.2/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D CUDA_ARCH_BIN="6.1" -D CUDA_ARCH_PTX="" -D CUDA_FAST_MATH=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D CUDA_GENERATION=Pascal -D BUILD_EXAMPLES=OFF ..

参考:https://blog.csdn.net/weixin_42271913/article/details/88997729(原文有对选项的详细解释)

 

4,make -j8 时报错
[ 34%] Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libavformat.a(allformats.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavformat.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:386: recipe for target 'lib/libopencv_videoio.so.3.4.2' failed
make[2]: *** [lib/libopencv_videoio.so.3.4.2] Error 1
CMakeFiles/Makefile2:11230: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
 

解决:标红部分不管是libavformat.a还是 libavcodec.a 、libavutil.a或libswscale.a引起的报错,都可以先把这几个文件从/usr/local/lib中移动别处先放着,等到编译完成了再放回去。     

参考:https://blog.csdn.net/qingyuanluofeng/article/details/51707929

 

5,make -j8 时报错
[ 88%] Linking CXX executable ../../bin/example_gpu_multi
/usr/bin/ld: CMakeFiles/example_gpu_multi.dir/multi.cpp.o: undefined reference to symbol '_ZN3tbb18task_group_contextD1Ev'
//usr/lib/x86_64-linux-gnu/libtbb.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_multi.dir/build.make:122: recipe for target 'bin/example_gpu_multi' failed
make[2]: *** [bin/example_gpu_multi] Error 1
CMakeFiles/Makefile2:47768: recipe for target 'samples/gpu/CMakeFiles/example_gpu_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_multi.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

解决:如果上面第3步中的BUILD_EXAMPLES=ON,请改成=OFF

参考:https://www.jianshu.com/p/b1df7df72b6d

 

6,至此看到下面这样的结果,编译完成。

...

[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python3
[100%] Built target opencv_python2

此时再把4中的几个文件放回到/usr/local/lib目录下,比如这样:

$ cd /usr/local/lib

$ sudo cp -r /home/xxx/cmake_files/lib* . # 我之前是把这些文件放在cmake_files文件夹下的,可以按照你自己的情况修改

======================================================================

 

 

 

附:完整操作过程如下:

# 1,从官网下载source文件(opencv-3.4.2.zip, opencv_contrib-3.4.2.zip),我用的wget方法,你可以尝试各种办法,只要能下载下来就行
$ wget https://github.com/opencv/opencv/archive/3.4.2.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.4.2.zip

# 2,分别解压到各自不同的文件夹,不用非得把opencv_contrib解压到opencv里面,只要后面cmake时指定好contrib的目录就行
$ unzip opencv-3.4.2.zip
$ unzip opencv_contrib-3.4.2.zip

# 3,进入解压后的目录,新建build文件夹
$ cd opencv-3.4.2
$ mkdir build
$ cd build

# 4,手动下载ippicv_2017u3_lnx_intel64_general_20180518.tgz和 face_landmark_model.dat这两个文件。保存在本地,如/home/xxx/cmake_files/文件夹下。下载地址分别如下:
https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20180518/ippicv/ippicv_2017u3_lnx_intel64_general_20180518.tgz
https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20180518/ippicv/ippicv_2017u3_lnx_intel64_general_20180518.tgz

# 5,参考正文1和2,修改opencv-3.4.2/opencv/3rdparty/ippicv/ippicv.cmake和opencv_contrib-3.4.2/modules/face/CMakeLists.txt这两个文件,把下载网址改成本地保存地址,如都修改为:
"file:///home/xxx/cmake_files/"

# 6,开始惊心动魄的cmake旅程。。(如有报错请参考正文或其他文章)
$ cmake .. # 这一步是必要的,官网的安装说明好像都没提这一步

# 我的显卡是titan xp,算力是6.1,所以CUDA_ARCH_BIN="6.1" 
# 我想使用python3,所以PYTHON_EXCUTABLE=/usr/bin/python3
# 我的contrib解压目录是/home/xxx/Downloads/opencv_contrib-3.4.2/modules
# 显卡是titan xp,架构为Pascal,所以CUDA_GENERATION=Pascal
# 为了解决正文5中提到的错误,设置:BUILD_EXAMPLES=OFF
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv/3.4.2 -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=/home/xxx/Downloads/opencv_contrib-3.4.2/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D CUDA_ARCH_BIN="6.1" -D CUDA_ARCH_PTX="" -D CUDA_FAST_MATH=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D CUDA_GENERATION=Pascal -D BUILD_EXAMPLES=OFF ..

$ make -j8 #我的cpu是8核的。过程中如果出现正文4提到的报错,就先把4个文件移动到别处,如:
# $ mv /usr/local/lib/libavformat.a /home/xxx/cmake_files/
# $ mv /usr/local/lib/libavcodec.a /home/xxx/cmake_files/
# $ mv /usr/local/lib/libavutil.a /home/xxx/cmake_files/
# $ mv /usr/local/lib/libswscale.a /home/xxx/cmake_files/
# 再运行make -j8
# 如果这样做了,记得在编译结束后,在下面的第7步之前把这四个文件再移动回来。

# 编译安装的最后一步
$ sudo make install

# $ cd /usr/local/lib
# $ sudo cp -r /home/xxx/cmake_files/lib* .

# 7,参考https://blog.csdn.net/weixin_42271913/article/details/88997729做一些后续配置:
$ sudo vi /etc/ld.so.conf.d/opencv.conf # 加入/usr/local/lib
$ sudo ldconfig

$ sudo vi /etc/bash.bashrc
# 在文件末尾添加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH

$ source /etc/bash.bashrc
$ sudo updatedb

# 8,安装完成之后的测试:
$ pkg-config --modversion opencv

# 输出:
3.4.2

还可以用这里(https://blog.csdn.net/qxqxqzzz/article/details/100749220)提到的程序进一步在gpu上进行测试!

你可能感兴趣的:(#,CV-基础,Linux,#,DL-报错)