visual studio code(vscode) c++ 編譯opencv並執行除錯
vscode的優點我想用過都知道,這裡只針對opencv編譯貼上一些配置資訊,對於用慣於IDE的我來說.配置這些還是要花一些時間去找資料的.
準備工作: 1.opencv的安裝已經完成(包插mingw-64,cmake)
2.vscode安裝並安裝cpptool支援外掛
vscode c++編譯器中文輸出亂碼,如printf()或者cout,
解決方法:將當前檔案的編碼格式改為gbk即可, vscode視窗的右下角,點選顯示的當前編碼格式,輸入gbk,儲存,重新f5 進入debug中文列印就是正常了
配置vscode專案:
launch.json,task.json,c_cpp_properties.json
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", //"program": "enter program name, for example ${workspaceFolder}/a.exe", "program": "${file}.o", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\mingw64\\bin\\gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
c_cpp_properties.json
{ "configurations": [ { "name": "Win32", "includePath": [ "D:/opencv/build/include",//這裡引入opencv原始檔路徑,下面兩個地址都是 "${workspaceFolder}/**", "D:/opencv/build/include/opencv", "D:/opencv/build/include/opencv2" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:\\MinGW\\mingw64\\bin\\gcc.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"type": "shell",
"command": "g++",
"args": [ "-g", "${file}", "-o", "${file}.o",//這裡新增動態連結庫
"-I","D:/opencv/build/include",
"-I","D:/opencv/build/include/opencv",
"-I","D:/opencv/build/include/opencv2",
"-L", "D:/opencv/build/x64/mingw/lib",
"-l", "opencv_core330",
"-l", "libopencv_imgproc330",
"-l", "libopencv_video330",
"-l", "libopencv_ml330",
"-l", "libopencv_highgui330",
"-l", "libopencv_objdetect330",
"-l", "libopencv_flann330",
"-l", "libopencv_imgcodecs330",
"-l", "libopencv_photo330",
"-l", "libopencv_videoio330"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
mian.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int arg, char** args) {
std::cout << "aa" << std::endl;
std::string img = "D:\\timg.jpg";
Mat srcImage = imread(img);
if (!srcImage.data) {
return 1;
}
imshow("srcImage", srcImage);
cvWaitKey(0);
return 0;
}
貼一個網上找的c++編譯執行命令
linux->
g++ -o test test.cpp -I/usr/local/include -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L /usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lpthread
引數解釋:
gcc -i 引數說明:包含標頭檔案 -include和-I引數
gcc -l 引數說明: libopencv_core330 完整dll檔名,上面args載入的就是D:/opencv/build/x64/mingw/lib/libopencv_core330.dll.a