Ubuntu vscode(c++)配置opencv4.1.0

https://blog.csdn.net/seniorc/article/details/112277769

本文转载于:Ubuntu18.04 vscode(c++)配置opencv successful
需要准备的东西:vscode vscode-c++编译环境 opencv,主要是修改三个文档,launch.json , c_cpp_properties.json , tasks.json
vscode配置c++网上的教程较多,在这里就不再赘述。
1、新建一个cpp文件,保存。
2、vscode左侧方的debug按钮(小虫子)–Debug -> Open Configurations -> 打开备选框 -> C++(GDB/LLDB) -> g++ build and debug active file
3、回到资源管理器(左侧栏的第一个图标)
在这里插入图片描述
4、上述操作后打开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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",//也就是添加一个launch之间的任务,任务名为build,这个build就是我们在tasks.json中设置的任务名。
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

5、按键ctrl+shift+P,后输入选择Configure Task,在备选框中选择C/C++:cpp build active file选项,则新建一个tasks.json文件。

{
    // 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": [
                "-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}.o",// 设置动态链接库
                "-I", "/usr/local/include",
                "-I", "/usr/local/include/opencv2",
                "-I", "/usr/local/include/opencv4",
                "-L", "/usr/local/lib",
                "-l", "opencv_core",
                "-l", "opencv_imgproc",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_video",
                "-l", "opencv_ml",
                "-l", "opencv_highgui",
                "-l", "opencv_objdetect",
                "-l", "opencv_flann",
                "-l", "opencv_imgcodecs",
                "-l", "opencv_photo",
                "-l", "opencv_videoio"
            ],// 编译命令参数        
            }
     ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6、Ctrl + Shift + P 打开搜索框,键入c++,会出现备选项目,选择图示Edit configurations (JSON)
并且将c_cpp_properties.json修改为:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include", //请确保你的opencv opencv2头文件夹安装在这个目录
                "/usr/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

opencv安装的目录要在/usr/local/include里面。
可以输入命令 :

cd /usr/local/include
ls
  • 1
  • 2

结果如下:
在这里插入图片描述

7,回到测试文件,F5运行如下测试程序:

#include <iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() 
{
    cout<<"hello world"<<endl;
    VideoCapture capture(0);
    while (1)
    {
        Mat frame;
        capture>>frame;
        imshow("src",frame);
        waitKey(1);
    }
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值