做测试时需要用OpenCV。虽然网络上有大量的关于编译OpenCV的教程,但是还是遇到了问题。因此记录了编译的过程,希望以后能更加顺利。
编译环境时Ubuntu 18.04。先在Ubuntu 18.04安装各种软件。
sudo apt-get update
sudo apt-get upgrade
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt install build-essential libgtk2.0-dev libjpeg-dev libjasper-dev libopenexr-dev libtbb-dev libvtk7-dev libtiff-dev libtiff5-dev libblas-dev liblapack-dev libopenblas-dev libatlas-base-dev libv4l-dev
准备下列头文件,否则OpenCV的configure会报告错误。
sudo ln -s /usr/include/libv4l1-videodev.h /usr/include/linux/videodev.h
sudo ln -s /usr/include/x86_64-linux-gnu/sys/videoio_c.h /usr/include/x86_64-linux-gnu/sys/videoio.h
使用Ubuntu自带的FFMpeg时,编译OpenCV出错。要从源代码编译FFMpeg,并且要使能pic标志。
通过git,获取FFMpeg源代码。使用FFMpeg master版本,编译opencv-3.4.16和opencv-4.5.4都失败。使用FFMpeg n4.2.5, 编译opencv-3.4.16和opencv-4.5.4,都能成功。
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
git tag -l n4.*
git checkout -b n4.2.5
使用FFMpeg master(2021年12月9日, 最新tag是n4.5-dev)版本,编译opencv-4.5.4失败,部分log如下:
[ 56%] Building CXX object modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/render/grenderocv.cpp.o
In file included from /proj/hankf/hankf/slam/opencv-4.5/opencv-4.5.4/modules/videoio/src/cap_ffmpeg.cpp:50:0:
/proj/hankf/slam/opencv-4.5/opencv-4.5.4/modules/videoio/src/cap_ffmpeg_impl.hpp:537:5: error: \u2018AVBSFContext\u2019 does not name a type; did you mean \u2018AVIOContext\u2019?
AVBSFContext* bsfc;
^~~~~~~~~~~~
AVIOContext
/proj/hankf/slam/opencv-4.5/opencv-4.5.4/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function \u2018void CvCapture_FFMPEG::init()\u2019:
/proj/hankf/slam/opencv-4.5/opencv-4.5.4/modules/videoio/src/cap_ffmpeg_impl.hpp:583:5: error: \u2018bsfc\u2019 was not declared in this scope
bsfc = NULL;
^~~~
使用FFMpeg master(2021年12月9日, 最新tag是n4.5-dev)版本,编译opencv-3.4.16失败,部分log如下:
[ 51%] Linking CXX shared library ../../lib/libopencv_shape.so
[ 51%] Built target opencv_shape
In file included from /proj/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg.cpp:49:0:
/proj/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg_impl.hpp:548:5: error: \u2018AVBSFContext\u2019 does not name a type; did you mean \u2018AVIOContext\u2019?
AVBSFContext* bsfc;
^~~~~~~~~~~~
AVIOContext
/proj/hankf/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function \u2018void CvCapture_FFMPEG::init()\u2019:
/proj/hankf/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg_impl.hpp:595:5: error: \u2018bsfc\u2019 was not declared in this scope
bsfc = NULL;
^~~~
/proj/hankf/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg_impl.hpp:991:47: error: \u2018AVStream {aka struct AVStream}\u2019 has no member named \u2018codec\u2019
AVCodecContext *enc = ic->streams[i]->codec;
^~~~~
/proj/hankf/hankf/slam/opencv-3.4/opencv-3.4.16/modules/videoio/src/cap_ffmpeg_impl.hpp:1040:45: error: invalid conversion from \u2018const AVCodec*\u2019 to \u2018AVCodec*\u2019 [-fpermissive]
codec = avcodec_find_decoder(enc->codec_id);
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
/proj/hankf/slam/ffmpeg$ pwd
/proj/hankf/slam/ffmpeg
/proj/hankf/slam/ffmpeg$ tree -L 1 -d
compat
doc
ffbuild
fftools
libavcodec
libavdevice
libavfilter
libavformat
libavresample
libavutil
libpostproc
libswresample
libswscale
presets
tests
tools
./configure --enable-nonfree --enable-pic --enable-shared
配置FFMpeg时,要使能pic标志,否则编译OpenCV出错。
libavcodec.a(hevc_cabac.o): relocation R_X86_64_PC32 against `ff_w_ff' can not be used when makin with -fPIC
下面是编译和安装FFMpeg的简要记录。
/proj/hankf/slam/ffmpeg$ make -j 64
LD libavfilter/libavfilter.so.8
LD libavdevice/libavdevice.so.59
LD ffmpeg_g
LD ffprobe_g
STRIP ffprobe
STRIP ffmpeg
/proj/hankf/slam/ffmpeg$ sudo make install
[sudo] password for hankf:
INSTALL libavdevice/libavdevice.a
INSTALL libavdevice/libavdevice.so
STRIP install-libavdevice-shared
......
INSTALL ffmpeg
INSTALL ffprobe
......
INSTALL libavutil/ffversion.h
INSTALL libavutil/libavutil.pc
编译和安装FFMpeg后,运行FFMpeg,得到错误“libavdevice.so.58: cannot open shared object file: No such file or directory”。
/proj/hankf/slam/ffmpeg$ ./ffmpeg -v
./ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory
默认情况下,FFMpeg的库文件放在目录/usr/local/lib下。把FFMpeg的lib路径加到文件/etc/ld.so.conf.d/ffmpeg.conf中,再执行ldconfig使配置生效,就能正常运行FFMpeg。
/proj/hankf/slam/ffmpeg$ cat /etc/ld.so.conf.d/ffmpeg.conf
/usr/local/lib
/proj/hankf/slam/ffmpeg$ sudo ldconfig
[sudo] password for hankf:
/proj/hankf/slam/ffmpeg$ ./ffmpeg -v
ffmpeg version n4.2.5 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-nonfree --enable-pic --enable-shared
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
从http://opencv.org/downloads.html下载OpenCV源代码,会得到一个文件名类似 opencv-3.4.16.zip 的压缩包。OpenCV源代码是cmake工程。
解压OpenCV后,得得下面的目录。OpenCV不允许在源代码目录编译,最好在父级目录创建一个build子目录。
/proj/hankf/slam/opencv-3.4$ pwd
/proj/hankf/slam/opencv-3.4
/proj/hankf/slam/opencv-3.4$ tree -L 2 -d
build
opencv-3.4.16
3rdparty
apps
cmake
CMakeFiles
data
doc
include
modules
platforms
samples
在父级目录中的build子目录下,执行cmake,配置OpenCV。部分log如下:
/proj/hankf/slam/opencv-3.4/build$ cmake ../opencv-3.4.16/
-- General configuration for OpenCV 3.4.16 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2021-12-09T08:24:47Z
-- Host: Linux 5.4.0-90-generic x86_64
-- CMake: 3.10.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/gmake
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (16 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (30 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (7 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++11: YES
-- C++ Compiler: /usr/bin/c++ (ver 7.5.0)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: dl m pthread rt
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo shape stitching superres ts video videoio videostab viz
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- GTK+: YES (ver 2.24.32)
-- GThread : YES (ver 2.56.4)
-- GtkGlExt: NO
-- VTK support: YES (ver 7.1.1)
--
-- Media I/O:
-- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
-- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
-- JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
-- OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_2)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: YES
-- avcodec: YES (ver 59.14.100)
-- avformat: YES (ver 59.9.102)
-- avutil: YES (ver 57.10.101)
-- swscale: YES (ver 6.1.101)
-- avresample: YES (ver 3.7.0)
-- GStreamer: NO
-- libv4l/libv4l2: NO
-- v4l/v4l2: linux/videodev.h linux/videodev2.h
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2020.0.0 Gold [2020.0.0]
-- at: /proj/hankf/hankf/slam/opencv-3.4/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2020.0.0)
-- at: /proj/hankf/hankf/slam/opencv-3.4/build/3rdparty/ippicv/ippicv_lnx/iw
-- Lapack: NO
-- Eigen: YES (ver 3.3.4)
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /proj/hankf/hankf/slam/opencv-3.4/opencv-3.4.16/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /proj/hankf/hankf/slam/opencv-3.4/build
配置过程中,还报告找不到OpenBLAS、Atlas。这两个消息不影响后续编译。
-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find Atlas (missing: Atlas_CLAPACK_INCLUDE_DIR)
继续在build目录下,执行命令“make -j 64", 编译OpenCV。部分log如下:
/proj/hankf/slam/opencv-3.4/build$ make -j 64
[ 0%] Building C object 3rdparty/quirc/CMakeFiles/quirc.dir/src/decode.c.o
[ 0%] Generate opencv.pc
[ 0%] Building C object 3rdparty/ittnotify/CMakeFiles/ittnotify.dir/src/ittnotify/jitprofiling.c.o
[ 0%] Building C object 3rdparty/quirc/CMakeFiles/quirc.dir/src/quirc.c.o
......
[100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/perf_estimators.cpp.o
[100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_warpers.cpp.o
[100%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/perf_matchers.cpp.o
[100%] Linking CXX executable ../../bin/opencv_traincascade
[100%] Built target opencv_test_calib3d
[100%] Linking CXX executable ../../bin/opencv_test_stitching
[100%] Linking CXX executable ../../bin/opencv_perf_stitching
[100%] Built target opencv_test_videostab
[100%] Built target opencv_traincascade
[100%] Built target opencv_createsamples
[100%] Built target opencv_perf_objdetect
[100%] Built target opencv_test_objdetect
[100%] Built target opencv_perf_core
[100%] Built target opencv_test_stitching
[100%] Built target opencv_perf_stitching
[100%] Built target opencv_test_dnn
[100%] Built target opencv_perf_imgproc
[100%] Built target opencv_test_imgproc
[100%] Built target opencv_test_core
继续在build目录下,执行命令“make -j 64", 编译OpenCV。部分log如下:
/proj/hankf/slam/opencv-3.4/build$ sudo make install
[sudo] password for hankf:
[ 0%] Built target gen-pkgconfig
[ 0%] Built target ittnotify
[ 3%] Built target ippiw
[ 13%] Built target opencv_core
[ 22%] Built target opencv_imgproc
[ 24%] Built target opencv_imgcodecs
[ 25%] Built target opencv_videoio
[ 25%] Built target opencv_highgui
[ 26%] Built target opencv_ts
[ 30%] Built target opencv_test_core
[ 33%] Built target opencv_perf_core
......
[ 96%] Built target opencv_perf_superres
[ 97%] Built target opencv_videostab
[ 97%] Built target opencv_test_videostab
[ 98%] Built target opencv_traincascade
[ 98%] Built target opencv_createsamples
[ 98%] Built target opencv_annotation
[ 99%] Built target opencv_visualisation
[100%] Built target opencv_interactive-calibration
[100%] Built target opencv_version
Install the project...
-- Install configuration: "Release"
......
-- Set runtime path of "/usr/local/bin/opencv_visualisation" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib"
用同样的方法配置OpenCV 4.5.4,然后编译OpenCV,也能编译成功。部分log如下:
[100%] Building CXX object modules/gapi/CMakeFiles/opencv_test_gapi.dir/test/util/variant_tests.cpp.o
[100%] Building CXX object modules/gapi/CMakeFiles/opencv_test_gapi.dir/test/streaming/gapi_streaming_vpl_core_test.cpp.o
[100%] Building CXX object modules/gapi/CMakeFiles/opencv_test_gapi.dir/test/util/optional_tests.cpp.o
[100%] Building CXX object modules/gapi/CMakeFiles/opencv_test_gapi.dir/test/util/any_tests.cpp.o
[100%] Linking CXX executable ../../bin/opencv_test_gapi
[100%] Built target opencv_perf_gapi
[100%] Built target opencv_test_gapi
重新编译的log如下:
/proj/hankf/slam/opencv-4.5/build$ make -j 64
[ 1%] Built target quirc
[ 1%] Built target ittnotify
[ 1%] Built target opencv_videoio_plugins
[ 1%] Built target opencv_highgui_plugins
[ 4%] Built target ippiw
[ 5%] Built target ade
[ 11%] Built target libprotobuf
[ 19%] Built target opencv_core
[ 19%] Built target opencv_version
[ 19%] Built target opencv_flann
[ 20%] Built target opencv_ml
[ 27%] Built target opencv_imgproc
[ 28%] Built target opencv_photo
[ 32%] Built target opencv_features2d
[ 32%] Built target opencv_imgcodecs
[ 42%] Built target opencv_dnn
[ 42%] Built target opencv_model_diagnostics
[ 43%] Built target opencv_videoio
[ 47%] Built target opencv_calib3d
[ 47%] Built target opencv_highgui
[ 47%] Built target opencv_visualisation
[ 47%] Built target opencv_annotation
[ 48%] Built target opencv_interactive-calibration
[ 49%] Built target opencv_objdetect
[ 50%] Built target opencv_stitching
[ 51%] Built target opencv_ts
[ 53%] Built target opencv_video
[ 54%] Built target opencv_perf_dnn
[ 54%] Built target opencv_test_flann
[ 56%] Built target opencv_test_features2d
[ 57%] Built target opencv_test_highgui
[ 57%] Built target opencv_perf_objdetect
[ 57%] Built target opencv_perf_imgcodecs
[ 58%] Built target opencv_test_photo
[ 59%] Built target opencv_perf_photo
[ 59%] Built target opencv_perf_features2d
[ 59%] Built target opencv_perf_calib3d
[ 59%] Built target opencv_test_objdetect
[ 60%] Built target opencv_test_ml
[ 60%] Built target opencv_test_imgcodecs
[ 61%] Built target opencv_perf_videoio
[ 62%] Built target opencv_test_stitching
[ 65%] Built target opencv_perf_core
[ 65%] Built target opencv_perf_stitching
[ 66%] Built target opencv_test_dnn
[ 68%] Built target opencv_perf_video
[ 68%] Built target opencv_test_video
[ 71%] Built target opencv_perf_imgproc
[ 72%] Built target opencv_test_videoio
[ 75%] Built target opencv_test_calib3d
[ 83%] Built target opencv_gapi
[ 87%] Built target opencv_test_core
[ 92%] Built target opencv_test_imgproc
[ 94%] Built target opencv_perf_gapi
[100%] Built target opencv_test_gapi