获取opencv源码
git clone git@github.com:opencv/opencv.git
获取opencv_contrib源码
git clone git@github.com:opencv/opencv_contrib.git
列举所有tag
git tag -l
这里以安装3.4.7为例
git checkout tags/3.4.7
opencv_contrib同样使用此方法检出对应的版本源码,注意版本号严格一致,否则会出现问题
为了避免污染文件,首先创建一个用于存放编译产生文件的目录
mkdir build
cd build
在该目录下运行cmake
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv347 -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ ..
- -DOPENCV_GENERATE_PKGCONFIG=ON opencv4默认不生产pc文件,需要开启此项
- -DCMAKE_INSTALL_PREFIX=/usr/local/opencv347 opencv的安装路径,可以省略,将安装到默认/usr/local目录
- -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ 将 opencv 和 opencv_contrib 一起编译,如果不需要 opencv_contrib 可以将此项去掉
然后是一段等待
成功后执行如下命令开始编译,这里开启了8个线程
make -j8
可能会出现如下错误
[ 8%] Building CXX object 3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/util/type_resolver_util.cc.o
[ 8%] Building CXX object 3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/wire_format.cc.o
[ 8%] Building CXX object 3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/wrappers.pb.cc.o
[ 8%] Linking CXX static library ../lib/liblibprotobuf.a
make[2]: 离开目录“/home/explorer/workbench/opensource/opencv/opencv/build”
[ 8%] Built target libprotobuf
make[1]: 离开目录“/home/explorer/workbench/opensource/opencv/opencv/build”
make: *** [Makefile:163:all] 错误 2
以及gcc fatal error
[ 1%] Building C object 3rdparty/ippiw/CMakeFiles/ippiw.dir/src/iw_image.c.o
In file included from /home/explorer/workbench/opensource/opencv/opencv/modules/core/src/precomp.hpp:55,
from /home/explorer/workbench/opensource/opencv/opencv/build/modules/core/opencv_core_pch_dephelp.cxx:1:
/home/explorer/workbench/opensource/opencv/opencv/modules/core/include/opencv2/core/private.hpp:66:12: 致命错误:Eigen/Core:没有那个文件或目录
66 | # include <Eigen/Core>
| ^~~~~~~~~~~~
编译中断。
make[2]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/build.make:63:modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.cxx.o] 错误 1
make[2]: 离开目录“/home/explorer/workbench/opensource/opencv/opencv/build”
make[1]: *** [CMakeFiles/Makefile2:3978:modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
[ 1%] Building C object 3rdparty/ippiw/CMakeFiles/ippiw.dir/src/iw_image_color_convert_all.c.o
cc1: 警告:‘-Werror=’ argument ‘-Werror=non-virtual-dtor’ is not valid for C
cc1: 警告:‘-Werror=’ argument ‘-Werror=non-virtual-dtor’ is not valid for C
In file included from /home/explorer/workbench/opensource/opencv/opencv/modules/core/test/test_precomp.hpp:12,
from /home/explorer/workbench/opensource/opencv/opencv/build/modules/core/opencv_test_core_pch_dephelp.cxx:1:
/home/explorer/workbench/opensource/opencv/opencv/modules/core/include/opencv2/core/private.hpp:66:12: 致命错误:Eigen/Core:没有那个文件或目录
66 | # include <Eigen/Core>
| ^~~~~~~~~~~~
编译中断。
make[2]: *** [modules/core/CMakeFiles/opencv_test_core_pch_dephelp.dir/build.make:63:modules/core/CMakeFiles/opencv_test_core_pch_dephelp.dir/opencv_test_core_pch_dephelp.cxx.o] 错误 1
make[2]: 离开目录“/home/explorer/workbench/opensource/opencv/opencv/build”
make[1]: *** [CMakeFiles/Makefile2:3868:modules/core/CMakeFiles/opencv_test_core_pch_dephelp.dir/all] 错误 2
[ 1%] Building C object 3rdparty/ippiw/CMakeFiles/ippiw.dir/src/iw_image_color_convert_rgbs.c.o
cc1: 警告:‘-Werror=’ argument ‘-Werror=non-virtual-dtor’ is not valid for C
In file included from /home/explorer/workbench/opensource/opencv/opencv/modules/ts/src/precomp.hpp:4,
from /home/explorer/workbench/opensource/opencv/opencv/build/modules/ts/opencv_ts_pch_dephelp.cxx:1:
/home/explorer/workbench/opensource/opencv/opencv/modules/core/include/opencv2/core/private.hpp:66:12: 致命错误:Eigen/Core:没有那个文件或目录
66 | # include <Eigen/Core>
| ^~~~~~~~~~~~
编译中断。
解决办法
sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen
https://github.com/opencv/opencv/issues/14868
编译到50%左右可能会遇到
libopencv_core.so.3.4.7: undefined reference to `cblas_dgemm'
https://github.com/opencv/opencv/issues/14323
cmake -D CMAKE_EXE_LINKER_FLAGS=-lcblas -D WITH_TBB=ON -D WITH_OPENMP=ON -D WITH_IPP=ON -D WITH_CUDA=OFF -D ENABLE_CXX11=1 -D BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv347 -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ ..
然后执行
sudo make install
配置
cd /etc/ld.so.conf.d/
sudo touch opencv347.conf
# 根据安装位置
sudo sh -c 'echo "/usr/local/opencv347/lib" > opencv347.conf'
sudo ldconfig
复制pc文件
sudo cp -f /usr/local/opencv347/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig/opencv347.pc
测试一下
pkg-config --modversion opencv347
测试程序
cpp
#include <opencv2/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
cout << "OpenCV version : " << CV_VERSION << endl;
cout << "Major version : " << CV_MAJOR_VERSION << endl;
cout << "Minor version : " << CV_MINOR_VERSION << endl;
cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
return 0;
}
cmake
cmake_minimum_required(VERSION 3.14)
project(opencvhelloworld)
set(CMAKE_CXX_STANDARD 14)
find_package( OpenCV 3.4.7 REQUIRED )
add_executable(opencvhelloworld main.cpp)
target_link_libraries( opencvhelloworld ${OpenCV_LIBS} )
运行后可以看到opencv版本号
cmakelist文件修改find_package( OpenCV 3.4.7 REQUIRED )
可以引入其他版本
cmake
cmake_minimum_required(VERSION 3.14)
project(opencvhelloworld)
set(CMAKE_CXX_STANDARD 14)
find_package( OpenCV 4 REQUIRED )
add_executable(opencvhelloworld main.cpp)
target_link_libraries( opencvhelloworld ${OpenCV_LIBS} )
这个是通过pacman安装的4.1.1,同样可以使用此方法引入
参考文献
- Arch / Manjaro Linux下 Opencv 编译 配置 查看
- 如何编译 opencv3 和 opencv_contrib(Linux)
- ubuntu16.04下opencv3.2和opencv_contrib编译安装
- OpenCV_contrib模块概述
- CMakeLists.txt添加opencv库注意事项
- CMake找不到opencv库解决办法
- CMakeLists.txt添加opencv库注意事项
- Set Opencv path for a c++ project using Cmake
- How to Have Multiple Versions of OpenCV Side by Side
- cmake简明使用指南
- linux下编译C++写的opencv程序
- Linux下使用自定义路径来运行OpenCV
- 源码包的安装与pkg-config
- pkg-config 维基百科,自由的百科全书
- 理解 Linux中的pkg-config 工具
- /etc/ld.so.conf.d/目录下文件的作用
- linux环境下安装opencv (c++) 编译undefined reference错误的解决办法
- c – 错误:’create’不是’cv :: Tracker’的成员
- 如何让gcc的编译错误信息以英文的方式输出
- 如何在Python和C ++中输出OpenCV版本号
- opencv 跑KCF跟踪 配置opencv_contrib过程中遇到的坑
-- found Intel IPP (ICV version): 2019.0.0 [2019.0.0 Gold]
-- at: /home/explorer/workbench/opensource/opencvfromgit/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
-- found Intel IPP Integration Wrappers sources: 2019.0.0
-- at: /home/explorer/workbench/opensource/opencvfromgit/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find Atlas (missing: Atlas_BLAS_LIBRARY)
-- Looking for sgemm_
-- Looking for sgemm_ - not found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Found BLAS: /lib64/libblas.so
-- Looking for cheev_
-- Looking for cheev_ - not found
-- Looking for cheev_
-- Looking for cheev_ - found
-- A library with LAPACK API found.
-- LAPACK(LAPACK/Generic): LAPACK_LIBRARIES: /lib64/liblapack.so;/lib64/libblas.so
-- LAPACK(LAPACK/Generic): Support is enabled.
Scanning dependencies of target opencv_phase_unwrapping
Scanning dependencies of target opencv_imgcodecs
/usr/bin/ld: ../../lib/libopencv_core.so.3.4.7: undefined reference to `cblas_zgemm'
/usr/bin/ld: ../../lib/libopencv_core.so.3.4.7: undefined reference to `cblas_sgemm'
/usr/bin/ld: ../../lib/libopencv_core.so.3.4.7: undefined reference to `cblas_dgemm'
/usr/bin/ld: ../../lib/libopencv_core.so.3.4.7: undefined reference to `cblas_cgemm'
collect2: 错误:ld 返回 1
make[2]: *** [apps/version/CMakeFiles/opencv_version.dir/build.make:87:bin/opencv_version] 错误 1
make[2]: 离开目录“/home/explorer/workbench/opensource/opencvfromgit/opencv/build”
make[1]: *** [CMakeFiles/Makefile2:20743:apps/version/CMakeFiles/opencv_version.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
make[2]: 离开目录“/home/explorer/workbench/opensource/opencvfromgit/opencv/build”
make[2]: 进入目录“/home/explorer/workbench/opensource/opencvfromgit/opencv/build”
[ 29%] Building CXX object modules/phase_unwrapping/CMakeFiles/opencv_phase_unwrapping.dir/src/histogramphaseunwrapping.cpp.o
make[2]: 离开目录“/home/explorer/workbench/opensource/opencvfromgit/opencv/build”
make[2]: 进入目录“/home/explorer/workbench/opensource/opencvfromgit/opencv/build”
[ 29%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/loadsave.cpp.o
[ 29%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/utils.cpp.o
[ 29%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/grfmt_base.cpp.o