在mac下用vscode做opencv相关开发的相关配置
例如, 有这样一段c++代码:
#include "Triangle.hpp"
#include "rasterizer.hpp"
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <opencv2/opencv.hpp>
如果不做相关配置, 在#include <opencv2/opencv.hpp>
这一行会显示include error错误
下面是解决办法
安装opencv
$ brew install opencv
第一次安装会非常耗时, 我花了半天, 因为opencv的相关以来非常多
安装过程中可能会出现错误而终端, 因为有些依赖包因为各种原因安装错误
根据错误信息, 单独安装失败的依赖包, 相关错误自行google
然后继续安装opencv, 如此反复, 直到安装成功
安装完成之后, 在vscode里#include <opencv2/opencv.hpp>
这一行, 错误信息会变得不一样, command + .
会提示将一个路径加入到includePath里
我提示的是"/usr/local/Cellar/opencv/4.5.3_2/include/opencv4"
, 在这个路径下的确是有opencv2/opencv.hpp
文件
没有include error信息了, 大功告成了吗?
我想做debug, F5执行, 却出现'opencv2/opencv.hpp' file not found
错误信息
#include <opencv2/opencv.hpp>
这一行也出现了一个黄色的~符号
这是为什么呢?
先说解决办法
安装pkg-config
$ brew install pkg-config
pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line rather than hard-coding values. This will be helpful for finding the correct linker flags for OpenCV. This will be more clear in the subsequent steps.
用下面的命令查看OpenCV的linker flags:
$ pkg-config --cflags --libs opencv4
就会得到:
-I/usr/local/opt/opencv/include/opencv4 -L/usr/local/opt/opencv/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
注:
pkg-config --cflags --libs opencv4
, 此命令可能会出现错误:
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
这是因pkg-config找不到opencv.pc文件, 这个文件在OpenCV安装路下: /usr/local/Cellar/opencv/4.5.3_2/lib/pkgconfig/
看看PKG_CONFIG_PATH
这个环境变量有没有此路径, 没有的话就加上:
$ export PKG_CONFIG_PATH=/usr/local/Cellar/opencv/4.5.3_2/lib/pkgconfig
或者直接执行:
$ pkg-config --cflags --libs /usr/local/Cellar/opencv/4.5.3_2/lib/pkgconfig/opencv4.pc
得到了linker flags有什么用呢?
配置编译参数:
在项目目录下.vscode/task.json
的args参数里, 加上最后一行:
"args": [
"-std=c++11",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"`pkg-config", "--cflags", "--libs", "/usr/local/Cellar/opencv/4.5.3_2/lib/pkgconfig/opencv4.pc`"
],
加上这一行之后, 就相当于用clang++编译时加上了OpenCV的相关参数
这样'opencv2/opencv.hpp' file not found
错误就没有了
includePath只是解决了VSCode的代码提示代码跳转问题, 真正运行clang++编译时是不依赖于includePath的, 还需要额外加上OpenCV的路径参数
参考资料
https://medium.com/@jaskaranvirdi/setting-up-opencv-and-c-development-environment-in-xcode-b6027728003
https://dev.to/rizz0s/my-vscode-c-opencv-macos-config-80k
https://github.com/Microsoft/vscode/issues/47538