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
#include
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