https://github.com/angel-star/vscode_OpenCV_template_for_Mac/tree/master
关于OpenCV的安装我这里就不再赘述了 记得用brew并指定相关版本
在安装成功后记得注意看它的提示文字
Warning: [email protected] dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
==> Caveats
[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have [email protected] first in your PATH run:
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc
For compilers to find [email protected] you may need to set:
export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
For pkg-config to find [email protected] you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig”
文章目录
- 项目目录
- c_cpp_properties.json
- launch.json
- settings.json
- task.json
- Makefile
- main.cpp
- 运行
- 调试
- 参考文章
在刚刚接触这个IDE的时候,用到了code-runner这款插件,然而经过多次尝试发现它只能运行单个文件,进入到设置(defaultSettings.json)当中不难发现:它的原理也就是帮我们在shell中输入命令而达到编译运行程序的目的。
...
// Set the executor of each language.
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
...
}
...
但是和上篇文章MAC visual Studio Code 运行 调试c/c++ 配置(完美解决)
不同,我们的程序不光要能执行以及调试单纯的算法程序,还要适用于需要引入头文件和链接库的中型甚至大型项目,那么这时候就该请出我们的神器
make
了,先不急 我们一点点看
项目目录
下面是项目的简单目录
c_cpp_properties.json
在include的过程当中如果有问题,IDE会提示你并自动生成这个文件,如果你是初学者没关系,直接在’
.vscode
文件夹中创建这个文件就可以,如果是自动生成的稍作修改就可以,如果最后不行就按照我的改,我已经踩了无数的坑了,期间看过很多文章与教程,然而这是最终的版本。(当然了opencv的安装位置和编译器位置可能会不同,请各位看官自行调整,灵活一些)
小tips:
在vscode的json配置文件中如果有不清楚的标签一定要把鼠标放在上面看一看说明,下同。
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/opt/[email protected]/include",
"/usr/local/Cellar/[email protected]/3.4.5/include"
],
"defines": [],
"macFrameworkPath": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/local/Cellar/[email protected]/3.4.5/include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
参数说明:
“includePath”
:后期需要添加的额外头文件路径
“compilerPath”:
编译器所在的文件路径
launch.json
这个文件是在debug模式中需要用到并会自动生成的,稍作修改 ,注意:标签
“program"
中文件后缀和自身系统有关。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "Build"
}
]
}
"preLaunchTask"
:
"build hello world"
,是默认
launch.json
中没有的,表示执行文件前需要的编译任务。具体的任务内容我们在
task.json
中定义。
settings.json
此settings是工作区设置, 另外还有一个用户设置,它们之间的区别和联系等相关知识请各位读者自行了解。
//settings.json
{
"python.pythonPath": "/Users/zjx/anaconda3/bin/python3",
"code-runner.executorMap": {
"c": "cd $dir && make && ./$fileNameWithoutExt && make clean",
"cpp": "cd $dir && make && ./$fileNameWithoutExt && make clean"
},
"editor.renderWhitespace": "all",
"editor.renderLineHighlight": "all",
"editor.formatOnSave": true,
"code-runner.runInTerminal": true,
"code-runner.ignoreSelection": true,
"code-runner.enableAppInsights": false,
"C_Cpp.updateChannel": "Insiders",
"[makefile]": {
"editor.insertSpaces": true
},
"C_Cpp.default.includePath": [
"/usr/local/Cellar/[email protected]/3.4.5/include"
]
}
task.json
首先快捷键
shift+command+p
打开Tasks: Configure Tasks,选择 Create tasks.json file from templates,此时会蹦出一个下拉列表,在下拉列表中选择Others,然后稍作修改如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-I",
"/usr/local/Cellar/[email protected]/3.4.5/include/opencv",
"-I",
"/usr/local/Cellar/[email protected]/3.4.5/include",
"-L",
"/usr/local/Cellar/[email protected]/3.4.5/lib",
"-l",
"opencv_stitching",
"-l",
"opencv_superres",
"-l",
"opencv_videostab",
"-l",
"opencv_aruco",
"-l",
"opencv_bgsegm",
"-l",
"opencv_bioinspired",
"-l",
"opencv_ccalib",
"-l",
"opencv_dnn_objdetect",
"-l",
"opencv_dpm",
"-l",
"opencv_face",
"-l",
"opencv_fuzzy",
"-l",
"opencv_hfs",
"-l",
"opencv_img_hash",
"-l",
"opencv_line_descriptor",
"-l",
"opencv_optflow",
"-l",
"opencv_reg",
"-l",
"opencv_rgbd",
"-l",
"opencv_saliency",
"-l",
"opencv_stereo",
"-l",
"opencv_structured_light",
"-l",
"opencv_phase_unwrapping",
"-l",
"opencv_surface_matching",
"-l",
"opencv_tracking",
"-l",
"opencv_datasets",
"-l",
"opencv_dnn",
"-l",
"opencv_plot",
"-l",
"opencv_xfeatures2d",
"-l",
"opencv_shape",
"-l",
"opencv_video",
"-l",
"opencv_ml",
"-l",
"opencv_ximgproc",
"-l",
"opencv_xobjdetect",
"-l",
"opencv_objdetect",
"-l",
"opencv_calib3d",
"-l",
"opencv_features2d",
"-l",
"opencv_highgui",
"-l",
"opencv_videoio",
"-l",
"opencv_imgcodecs",
"-l",
"opencv_flann",
"-l",
"opencv_xphoto",
"-l",
"opencv_photo",
"-l",
"opencv_imgproc",
"-l",
"opencv_core",
"-g"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
上面那一长串究竟是怎么来的呢 在终端中输入以下命令试试
pkg-config opencv --libs —cflags
按照json格式改好了就可以写进去了,别问为啥,学。
Makefile
TARGET = ./main
SRCS := $(wildcard ./src/*.cpp ./*.cpp)
OBJS := $(patsubst %cpp,%o,$(SRCS))
CFLG = -g -Wall -I/usr/local/Cellar/[email protected]/3.4.5/include -Iinc -I./ -std=c++11
LDFG = -Wl, $(shell pkg-config opencv --cflags --libs)
CXX = g++
$(TARGET) : $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LDFG)
%.o:%.cpp
$(CXX) $(CFLG) -c $< -o [email protected]
.PHONY : clean
clean:
-rm ./*.o
main.cpp
// main.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main(int argc, char **argv)
{
Mat img = cv::imread(argv[1], -1);
if (img.empty())
return -1;
cv::namedWindow("Example1", cv::WINDOW_AUTOSIZE);
cv::imshow("Example1", img);
cv::waitKey(0);
cv::destroyWindow("Example1");
return 0;
// Mat img = imread("/Users/zjx/Desktop/123.png");c
// cv::imshow("image", img);
// cv::waitKey();
// return 0;
}
运行
control + option + N
利用code-runner插件进行快速运行:
Tips :对于本程序的话 ,记得传参,可以修改makefile也可以修改settings也可以编译后在终端中运行并传参,看个人喜好
调试
shift + command + B
进行编译 然后
F5
启用调试 或者直接在左边调试窗口按下绿色三角按钮,相当于
编译+启动调试
,如下图 完美解决。
参考文章
跟我一起写Makefile
Ubuntu下程序的编译和链接过程和openCV的makefile文件
ubuntu14.04+opencv 3.0+python2.7安装及测试
opencv的第一份代码,及其makefile通用格式
Makefile Templates
Makefile的使用方法
Visual Studio Code (VSCode) 之 C/C++ 调试配置详解
vs code 配置.json文件引入makefile文件实现多文件编译
VSCode的Python扩展下程序运行的几种方式与环境变量管理
Python下如何配置Code Runner 可以直接启动调试?
VS code 运行&调试opencv C++配置
【转】gcc/g++常用编译选项和gdb常用调试命令
Configuring launch.json for C/C++ debugging
C/C++ for Visual Studio Code (Preview)
MAC+VS Code+C/C++调试配置
vscode下调试运行c++
VScoder C++ opencv 配置
gcc/g++参数详解
最近这几天真的在全身心解决这个问题(从这么多的参考文章中能否看出来么=。= ),因为网上大多数教程都是针对windows的 就算是mac环境下 xcode的教程也是更多一些,然后买了个相关课程,希望能从中找到关于环境搭建方面的技巧与经验,结果!老师说:大家就用windows吧,用linux和MAC的都是高手,因为要掌握cmake神马的balabala。。。当时很想放弃,结果还是熬过来了 现在回头看看虽然这些东西不难,但是这期间确实学到了很多东西,也希望大家在今后的学习生活中不要被困难打倒。
共勉。