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。。。當時很想放棄,結果還是熬過來了 現在回頭看看雖然這些東西不難,但是這期間确實學到了很多東西,也希望大家在今後的學習生活中不要被困難打倒。
共勉。