linux上编译运行包含opencv的c++程序
前言走了不少弯路,好在最终换个方式解决了。网上的资料错综复杂,让人看了很是头大。我原打算在win10上结合mingw和vscode写c++程序调用opencv,但是在千难万险之后,编译出程序后,运行程序却报错找不到库,我身疲力竭,果断转向linux。操作步骤安装预编译的opencvc++版本的只需要sudo apt install libopencv-dev即可。安装opencv的p...
前言
走了不少弯路,好在最终换个方式解决了。
网上的资料错综复杂,让人看了很是头大。
我原打算在win10上结合mingw和vscode写c++程序调用opencv,但是在千难万险之后,编译出程序后,运行程序却报错找不到库,我身疲力竭,果断转向linux。
操作步骤
安装预编译的opencv
c++版本的只需要sudo apt install libopencv-dev
即可。
安装opencv的python扩展sudo apt install python3-opencv
即可。
运行dpkg -L libopencv-dev
后,可以从中发现include文件在/usr/include下,lib文件在/usr/lib下,最重要的是有/usr/lib/x86_64-linux-gnu/pkgconfig/opencv.pc,我们可以使用pkg-config解决编译时的头文件和库问题。
/.
/usr
/usr/bin
/usr/bin/opencv_annotation
/usr/bin/opencv_createsamples
/usr/bin/opencv_interactive-calibration
/usr/bin/opencv_traincascade
/usr/bin/opencv_version
/usr/bin/opencv_visualisation
/usr/bin/opencv_waldboost_detector
/usr/include
/usr/include/opencv
/usr/include/opencv/cv.h
/usr/include/opencv/cv.hpp
/usr/include/opencv/cvaux.h
/usr/include/opencv/cvaux.hpp
/usr/include/opencv/cvwimage.h
/usr/include/opencv/cxcore.h
/usr/include/opencv/cxcore.hpp
/usr/include/opencv/cxeigen.hpp
/usr/include/opencv/cxmisc.h
/usr/include/opencv/highgui.h
/usr/include/opencv/ml.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/opencv.pc
/usr/share
/usr/share/OpenCV
/usr/share/OpenCV/OpenCVConfig-version.cmake
/usr/share/OpenCV/OpenCVConfig.cmake
/usr/share/OpenCV/OpenCVModules-release.cmake
/usr/share/OpenCV/OpenCVModules.cmake
/usr/share/doc
/usr/share/doc/libopencv-dev
/usr/share/doc/libopencv-dev/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/opencv_createsamples.1.gz
/usr/share/man/man1/opencv_haartraining.1.gz
/usr/share/man/man1/opencv_performance.1.gz
/usr/share/man/man1/opencv_traincascade.1.gz
/usr/share/doc/libopencv-dev/changelog.Debian.gz
编译
在正常的gcc编译语句后添加
`pkg-config --cflags --libs opencv`
`一般是tab键位上方的
使用pkg-config时,选项–cflags 它是用来指定程序在编译时所需要头文件所在的目录,选项 --libs则是指定程序在链接时所需要的动态链接库的目录。
其效果是
-I/usr/include/opencv -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
示例:
gcc Test.c -o Test `pkg-config --cflags --libs opencv`
编译目录下所有cpp文件的makefile
其中cpp文件彼此之间并无依赖与调用关系
makefile中"模式规则"的引入和介绍------%:%.cpp
CXX = g++
CFLAGS = -Wall
LDFLAGS = `pkg-config --cflags --libs opencv`
DBG_ENABLE=0
ifeq (1, ${DBG_ENABLE})
CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
endif
SRCS = $(wildcard *.cpp)
TARGETS = $(patsubst %.cpp, %,$(SRCS))
all:clean build
build:$(TARGETS)
$(TARGETS):%:%.cpp
$(CXX) $< -o $@ $(CFLAGS) $(LDFLAGS)
clean:
-rm -rf $(TARGETS)
.PHONY: clean all
其中句首的空白位置是tab。
参考资料
更多推荐
所有评论(0)