linux环境下的vscode+opencv+c++配置

一、新建test.cpp测试代码

#include
#include
using namespace cv;
int main(int argc,char** argv)
{
    //读取图片
    Mat img1=imread("34.jpg",cv::IMREAD_UNCHANGED);//原图格式读取
    Mat img2=imread("35.jpg",cv::IMREAD_UNCHANGED);//原图格式读取
    imshow("窗口1", img1);  //在窗口1中显示图片img(窗口大小不能调整)
    imshow("窗口2", img2);  
    waitKey(0);   
    return 0;
}

二、配置launch.json

点击vscode左侧debug and run 按钮,找到launch,将以下内容替换到launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "preLaunchTask": "build c program", 
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", 
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        }
    ]
}

其中,${workspaceFolder}表示当前工作目录,{fileBasenameNoExtension}表示当前文件名,不包括扩展"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",表示生成的执行文件在当前目录下,和当前文件同名,只是扩展为.o

三、修改task.json

打开tasks.json的方法:Ctrl +Shift +P ,输入task,选择Tasks:Configure Task

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",	/* 定义任务是被作为进程运行还是在 shell 中作为命令运行,默认是shell,即是在终端中运行,因为终端执行的就是shell的脚本 */
			"label": "build c program",	/* 要与launch.json文件里的preLaunchTask的内容保持一致 */
			"command": "/usr/bin/g++",	/* 这里填写你的编译器地址 */
			"args": [
				/* 类似与qt的Pro文件里开始的那几句 */
				"-std=c++11",// 静态链接
                "-static-libgcc",
				"-Wall",// 开启额外警告
				
				/* 说明整个项目所需的头文件路径(.h)*/
				"-I","${workspaceFolder}/",
                "-I","/usr/local/include/",
                "-I","/usr/local/include/opencv2/",
                "-I","/usr/local/include/opencv/",
 
				/* 说明整个项目所需的源文件路径(.cpp) */
                "-g",
                "${fileDirname}/*.cpp",/* ${workspaceFolder}表示路径从当前项目文件夹开始 */
 
				"-o",	/* 编译输出文件的存放路径 */
				"${fileDirname}/${fileBasenameNoExtension}", /* 要与launch.json文件里的program的内容保持一致 */
				/* ${fileDirname} 是指 文件目录名 相当于${workspaceFolder}*/
				/* ${fileBasenameNoExtension}意思是指 该路径下没有扩展名的文件基本名称没有扩展名 */ 
				/* 也可以这样:"${workspaceFolder}/run.o", */
 
				/* OpenCV的lib库 */
				"/usr/local/lib/libopencv_*",
			],
			"options": {
                "cwd": "${workspaceFolder}"	/* 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录 */
			},
			"problemMatcher": [
				"$gcc"	/* 要使用的问题匹配程序。 */
			],
			"group": "build" /* 将任务标记为可通过 "运行生成任务" 命令访问的生成任务。*/
		}
	]
}

四、修改c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/",
                "/usr/local/include/opencv2/",
                "/usr/local/include/opencv/"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

五、运行

点击F5,即可imshow图像

六、特别注意

6.1  tasks.json的"label"参数值和launch.json的"preLaunchTask"参数值需要保持一致

6.2  注意将配置文件中opencv的路经修改为本机路径

(此处是

"/usr/local/include/",

"/usr/local/include/opencv2/",

"/usr/local/include/opencv/"

)

你可能感兴趣的:(c++,opencv,ubuntu)