- 动态库
- 1.依赖环境
- opencv.pc
- ubuntu opencv build
- message(STATUS “OpenCV library status:”)
- message(STATUS “ version: ${OpenCV_VERSION}”)
- message(STATUS “ libraries: ${OpenCV_LIBS}”)
- message(STATUS “ include path: ${OpenCV_INCLUDE_DIRS}”)
- message(STATUS “ LD_LIBRARY_PATH path: ${LD_LIBRARY_PATH}”)
- # Add OpenCV headers location to your include paths
- include_directories(${OpenCV_INCLUDE_DIRS})
- target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} )
- 编译opencv
- 增加头文件路径
- 链接opencv静态库
- ubuntu opencv build
- # Add OpenCV headers location to your include paths
- #编译opencv
- add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0)
- #增加头文件路径
- include_directories(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0/include)
- #链接opencv静态库
- target_link_libraries(demo ${opencv_libs} -lrt -lpthread)
动态库
1.依赖环境
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j8
sudo make install
1. 打开ld.so.conf文件
sudo gedit /etc/ld.so.conf
最文件后新加一行:
include /usr/local/lib
2. 运行
sudo ldconfig
3. 修改bash.bashrc文件
sudo gedit /etc/bash.bashrc
在文件末尾加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
然后运行:
source /etc/bash.bashrc
验证安装
运行以下command查看安装版本,至此安装完成。
pkg-config opencv --modversion
- pkg-config opencv —modversion
cd /usr/local/lib sudo mkdir pkgconfig cd pkgconfig sudo touch opencv.pc
opencv.pc
``` prefix=/usr/local exec_prefix=${prefix} includedir=${prefix}/include libdir=${exec_prefix}/lib
Name: opencv Description: The opencv library Version:4.0.1 Cflags: -I${includedir}/opencv4 Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
<a name="HZB1A"></a>
## cmake file
cmake_minimum_required(VERSION 3.0) # 设置cmake版本 project(hair_color) # 设置工程名字
ubuntu opencv build
find_package( OpenCV REQUIRED )
message(STATUS “OpenCV library status:”)
message(STATUS “ version: ${OpenCV_VERSION}”)
message(STATUS “ libraries: ${OpenCV_LIBS}”)
message(STATUS “ include path: ${OpenCV_INCLUDE_DIRS}”)
message(STATUS “ LD_LIBRARY_PATH path: ${LD_LIBRARY_PATH}”)
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp ) #生成可执行文件
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} )
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} )
target_link_libraries( ${PROJECT_NAME} )
<a name="R2CXi"></a>
#
<a name="BcOWL"></a>
# 静态库
<a name="n1Nr1"></a>
## 参考
[https://blog.csdn.net/a731062834/article/details/115232225](https://blog.csdn.net/a731062834/article/details/115232225)<br />[https://www.jianshu.com/p/ee7273af2914](https://www.jianshu.com/p/ee7273af2914)<br />将 OpenCV 编译成静态库 cmake .. -DBUILD_SHARED_LIBS=NO<br />以OpenCV-4.0.0为例说明在Ubuntu环境下的静态库编译与链接方法:
<a name="AEO2a"></a>
### 1、编译配置
opencv依赖第三方的库,所以当把opencv编译成静态库的时候,注意要把它运行时必要的依赖库也编译进来,opencv源码内有自己的3rdparty,主要有:libjpeg、libpng、libtiff、libzlib、libwebp、libprotobuf、ffmpeg(用到video时编译)、libgtk(用于显示界面显示,服务器部署不会用到),可以通过cmake -LA来查看配置的编译信息,再根据信息考虑把opencv的哪些module编译进来。
cmake -DEXECUTABLE_OUTPUT_PATH:PATH=$(pwd) \ -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX=/usr/local \ -DBUILD_JASPER=ON \ -DBUILD_JAVA=OFF \ -DBUILD_JPEG=ON \ #编译opencv 3rdparty自带的libjpeg -DBUILD_PERF_TESTS=OFF \ -DBUILD_PNG=ON \ #编译opencv 3rdparty自带的libpng -DBUILD_PROTOBUF=ON \ #编译opencv 3rdparty自带的libprotobuf -DBUILD_SHARED_LIBS=NO \ #编译成静态库 -DBUILD_TESTS=OFF \ -DBUILD_TIFF=ON \ #编译opencv 3rdparty自带的libtiff -DBUILD_ZLIB=ON \ #编译opencv 3rdparty自带的libzlib -DBUILD_WEBP=ON \ #编译opencv 3rdparty自带的libwebp -DBUILD_opencv_apps=OFF \ -DBUILD_opencv_core=ON \ -DBUILD_opencv_calib3d=ON \ -DBUILD_opencv_dnn=ON \ -DBUILD_opencv_features2d=ON \ -DBUILD_opencv_flann=ON \ -DBUILD_opencv_gapi=OFF \ -DBUILD_opencv_highgui=ON \ -DBUILD_opencv_imgcodecs=ON \ -DBUILD_opencv_imgproc=ON \ -DBUILD_opencv_java_bindings_generator=OFF \ -DBUILD_opencv_js=OFF \ -DBUILD_opencv_ml=ON \ -DBUILD_opencv_objdetect=OFF \ -DBUILD_opencv_photo=OFF \ -DBUILD_opencv_python2=OFF \ -DBUILD_opencv_python3=OFF \ -DBUILD_opencv_python_bindings_generator=OFF \ -DBUILD_opencv_stitching=OFF \ -DBUILD_opencv_ts=OFF \ -DBUILD_opencv_video=OFF \ -DBUILD_opencv_videoio=OFF \ -DWITH_GTK=OFF \ -DWITH_GTK_2_X=OFF \ -DWITH_LAPACK=OFF \
<a name="0ZSbx"></a>
### 2、链接
链接时指定opencv的静态库以及依赖的库。另外需要注意头文件的问题,通过CMakeLists把opencv加入项目编译时,由于opencv编译完成后要install才会把所用的头文件集中拷贝到一个地方,所以事先将头文件拷贝到了项目的opencv目录下。
set(opencv_libs #项目用到的库 opencv_core opencv_dnn opencv_imgproc opencv_imgcodecs opencv_ml opencv_calib3d opencv_highgui zlib libjasper libjpeg-turbo libpng libtiff IlmImf libprotobuf libwebp)
编译opencv
add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0)
增加头文件路径
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0/include)
链接opencv静态库
target_link_libraries(demo ${opencv_libs} -lrt -lpthread)
CMAKE_MINIMUM_REQUIRED(VERSION 3.1) SET(PROJECT_NAME opencv_test) #opencv_test 是你项目名字,可以自己修改
PROJECT(${PROJECT_NAME}) set(OpenCV_DIR ~/work/opencv-4.5.0_build_static/build) #你之前源码包里的build文件夹,防止版本冲突 find_package(OpenCV 4 REQUIRED) #寻找这个包 opencv 包名 4 最低版本号 REQUIRED 如果没找到就报错
aux_source_directory(src DIR_SRCS) # src 是你存放源码的文件夹,如果在本目录aux_source_directory(. DIR_SRCS) add_executable(${PROJECT_NAME} ${DIR_SRCS}) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
原文链接:https://blog.csdn.net/qq_38032876/article/details/108869259
完整代码
cmake_minimum_required(VERSION 3.0) # 设置cmake版本 project(hair_color) # 设置工程名字
ubuntu opencv build
message(STATUS “ version: ${OpenCV_VERSION}”) message(STATUS “ libraries: ${OpenCV_LIBS}”) message(STATUS “ include path: ${OpenCV_INCLUDE_DIRS}”) message(STATUS “ LD_LIBRARY_PATH path: ${LD_LIBRARY_PATH}”)
# Add OpenCV headers location to your include paths
include_directories(${MNN_ROOT}) set(OpenCV_DIR /media/wwf/data/ubuntuSoft/opencv-4.3.0/static_release ) #你之前源码包里的build文件夹,防止版本冲突 find_package(OpenCV 4 REQUIRED) #寻找这个包 opencv 包名 4 最低版本号 REQUIRED 如果没找到就报错 set(opencv_libs #项目用到的库 opencv_core opencv_dnn opencv_imgproc opencv_imgcodecs opencv_ml opencv_calib3d opencv_highgui zlib libjasper libjpeg-turbo libpng libtiff IlmImf libprotobuf libwebp)
#编译opencv
add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0)
#增加头文件路径
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/opencv-4.0.0/include)
#链接opencv静态库
target_link_libraries(demo ${opencv_libs} -lrt -lpthread)
add_executable(${PROJECT_NAME} main.cpp ) #生成可执行文件
target_link_libraries(${PROJECT_NAME} ${opencv_libs} -lrt -lpthread )
```