当前位置:网站首页>How to introduce opencv into cmake project
How to introduce opencv into cmake project
2022-04-23 03:38:00 【Blue area soldier】
One 、OpenCV download
- opencv Download address
- opencv_contrib Download address
Be careful opencv as well as opencv_contrib The version of the best corresponding
Two 、OpenCV Compilation and installation
2.1 cmake Compilation and installation
After downloading , Enter the source directory ( namely CMakeLists.txt The same level directory )
- newly build build Catalog
mkdir build
- function cmake command , Pay attention to OpenCV Compilation of , The command has many options , Here are only the ones I often use
sudo cmake -D CMAKE_INSTALL_PREFIX=/media/jaychen/Config/Deepin/local/libs/opencv/4.5_vino \
-D CMAKE_BUILD_TYPE=DEBUG \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=/media/jaychen/Config/Deepin/local/libs/opencv_contrib/modules \
-D WITH_FFMPEG=ON ..
Here is an example of installation options
* Select installation location
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv
* Select the compilation method
-D CMAKE_BUILD_TYPE=RELEASE/DEBUG
* Whether to generate PKGCONFIG Needed opencv.pc file
-D OPENCV_GENERATE_PKGCONFIG=ON/OFF
* Whether to compile Contrib modular ( there PATH To write contrib Under the modules)
-D OPENCV_EXTRA_MODULES_PATH=/home/..../opencv_contrib/modules
* Is it just a opencv_world Dynamic link library
-D BUILD_OPENCV_WORLD=ON/OFF
* Whether to use Qt As highgui Display frame of module ( You need to install Qt)
-D WITH_Qt=ON/OFF
* Whether to use FFMPEG Make a video IO
-D WITH_FFMPEG=ON/OFF
- function make command
- function sudo make install command ( If installed to /usr/llib Wait for the location to need root jurisdiction )
2.2 Linux Direct installation
This way there is no FindOpenCV.cmake as well as OpenCVConfig.cmake. Only opencv.pc, It's through pkg-config How to organize the library .
sudo apt-get install libopencv-dev
3、 ... and 、 stay Cmake Introduction in OpenCV
3.1 Use OpenCVConfig.cmake
First set up OpenCV_DIR This variable is OpenCVConfig.cmake Directory of files , Then you can use find_package Command found opencv library , You can use it later include_directories as well as target_link_libraries The command introduces opencv The library of .cmake The script code is as follows :
set(OpenCV_DIR xxxx) # xxxx Directory contains OpenCVConfig.cmake
find_package(opencv required) # find opencv library
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} *.cpp) # *.cpp Refers to the source files to be compiled
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
3.2 Use FindOpenCV.cmake
And 3.1 similar
3.3 Use opencv.pc
cmake_minimum_required(VERSION 3.17)
project(C__test)
set(CMAKE_CXX_STANDARD 14)
set(ENV{
PKG_CONFIG_PATH} /usr/lib/x86_x64-linux-gnu/pkgconfig)
find_package(PkgConfig REQUIRED)
pkg_check_modules(OPENCV REQUIRED IMPORTED_TARGET opencv)
#MESSAGE(STATUS ${OPENCV_LIBRARIES})
if(${OPENCV_FOUND})
MESSAGE(${PKG_CONFIG_EXECUTABLE})
MESSAGE(${OPENCV_LIBRARIES})
MESSAGE(${OPENCV_LINK_LIBRARIES})
endif()
add_executable(C__test main.cpp) # Be careful add_executable Put it on target_link_libraries In front of , Otherwise, it will report a mistake
# Note that you can directly use PkgConfig::OPENCV This way, CMake file , FindPkgConfig There's something about , Baidu cmake pkgconfig doc There is .
target_link_libraries(${PROJECT_NAME} PkgConfig::OPENCV)
Four 、 stay Linux Middle switch OpenCV Version of
Sometimes different programs need different versions of OpenCV, Compiled and installed multiple OpenCV To different places , You need to switch different versions of the system link . It's recorded below Linux Several ways for the system to find the dynamic link library
Linux Several ways to find dynamic link libraries
By default , The compiler will only use /lib and /usr/lib The library files in these two directories
- LD_LIBRARY_PATH
This is a global variable - rpath
gcc Compile options rpath=xxx, Such as gcc -L. -Wl,-rpath=/home/bow/all/program/test/lib_version_test -o test main.o -ltest - ld.so.conf
Generally located /etc/ld.so.conf, Sometimes the content is include /etc/ld.so.conf.d/*.conf, Direct contains /etc/ld.so.conf.d All the conf file
conf The absolute path of the directory of the record library in the file , And by the /usr/lib/ld The program uses this configuration file - pkg-config Program
pkg-config The program is to configure A program that provides system information , For example, software version , Library version , Library path, etc . install pkg-config There will be a PKG_CONFIG_PATH environment variable , This environment variable stores *.pc Directory path of file ,.pc Files provide all kinds of information about the library ,PKG_CONFIG_PATH Search takes precedence over the default .
https://blog.csdn.net/weixin_38907560/article/details/81449272
./configure Commonly used pkg-config To check the installation environment
pkg-config --cflags opencv You can see opencv The header directory of
pkg-config --libs opencv You can see opencv Library file directory
Pay attention to opencv Is in accordance with the opencv.pc To find the name of , If in PKG_CONFIG_PATH Cannot find... In environment variable , Will go to its default path, such as /usr/local/lib/pkgconfig Next
版权声明
本文为[Blue area soldier]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220602025302.html
边栏推荐
- Problem B: small challenge
- 2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
- Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure
- ROS series (III): introduction to ROS architecture
- Visual programming - Experiment 2
- 標識符、關鍵字、數據類型
- ROS series (4): ROS communication mechanism series (4): topic communication practice
- MySQL is completely uninstalled and MySQL service is cleaned up
- File upload vulnerability summary and upload labs shooting range documentary
- VS Studio 修改C语言scanf等报错
猜你喜欢

SQL learning record

Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN

Visual programming - Experiment 2

The art of concurrent programming (2): synchronized usage scenarios

Design and implementation of redis (1): understand data structures and objects

Applet - canvas drawing Poster

Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics

Wechat applet cloud database value assignment to array error

PYMOL-note

L3-011 direct attack Huanglong (30 points)
随机推荐
Oracle JDK vs OpenJDK
Wechat applet canvas draws a simple asymptotic color of the dashboard
PYMOL-note
Identifier and type conversion
常用的辅助类
Design and implementation of redis (5): master-slave replication strategy and optimization
Chapter 8 exception handling, string handling and file operation
SQL learning record
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
ROS series (III): introduction to ROS architecture
MySQL is completely uninstalled and MySQL service is cleaned up
The content of the website is prohibited from copying, pasting and saving as JS code
Section 2 map and structure in Chapter 6
Use the thread factory to set the thread name in the thread pool
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Visual programming - Experiment 2
Application and definition of interface
ROS series (IV): ROS communication mechanism series (2): Service Communication
抽象类、接口、常用关键字