Ubuntu20.04/Ubuntu22.04 配置VScode+Opencv+cmake(C++)

Ubuntu20.04/Ubuntu22.04 配置VScode+Opencv+cmake(C++)


下面介绍Ubuntu20.04下安装opencv,当然Ubuntu22.04也适用,然后将opencv链接到VsCode
先主体按照—>点我:链接1<—的第一点进行安装,但是特别注意:安装依赖库的时候会失败,需要增加源,再把下列命令替换

/*增加源*/
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"

/*需要更换的代码*/
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

替换为下列命令

sudo apt install build-essential cmake unzip pkg-config
sudo apt install libjpeg-dev libpng-dev libtiff-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libv4l-dev libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install libatlas-base-dev gfortran

如果你想让你的opencv配置有cuda加速、cudnn的功能,先确保你的电脑已经安装好了cuda和cudnn(按这连接步骤配置好),然后在按那文章执行cmake命令时,需要将cmake命令替换掉opencv_contrib可以到( 点我跳转)下载,至于DCUDA_ARCH_BIN的取值需根据自己的显卡型号设置其值,不懂的可以来参考这文章( 点我跳转)

/*需要替换的命令*/
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..

/*替换成下列代码*/
/*因为我的是RTX2060,所以DCUDA_ARCH_BIN为7.5 */
sudo cmake -DCMAKE_BUILD_TYPE=Release \
 -DOPENCV_GENERATE_PKGCONFIG=ON \
 -DOPENCV_EXTRA_MODULES_PATH=<你的opencv_contrib的路径>/modules \
 -DWITH_CUDA=ON -DWITH_CUDNN=ON \
 -DOPENCV_DNN_CUDA=ON \
 -DBUILD_TESTS=False \
 -DCUDA_ARCH_BIN=7.5 ..

在这里插入图片描述

这时候我们依赖库已经安装好了,可以选择不装python的支持,毕竟我们是C++的嘛~
安装好后,我们需要配置c_cpp_properties.json、launch.json、tasks.json(如果vscode没有这三个文件,先自行百度怎么弄出这三个文件来)
在这里插入图片描述

1. 第一个版本
  此版本较繁琐,要在lauch.json写一大堆的include文件,也是网络上目前交流行的版本,虽然可正常使用opencv的库,

但此方法不推荐使用。接下来我贴出这个版本的三个文件的配置来
c_cpp_properties.json

/** c_cpp_properties.json **/
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",               
                "/usr/include/opencv4", //请确保你的opencv opencv2头文件夹安装在这个目录
                "/usr/include/opencv4/opencv2"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4

launch.json


{
    "version": "0.2.0",
    "configurations": [        
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",                                 // 只能是cppdbg
            "request": "launch",                              // launch:启动,attach:附加
            "program": "${fileDirname}/${fileBasenameNoExtension}",   // 需要调试的程序
            "args": [],                                       // 调试时传递给程序的参数
            "stopAtEntry": false,                             // 调试时是否停在程序入口:{true:是,false:否}
            "cwd": "${workspaceFolder}",                      // 工作目录
            "environment": [],                                // 额外的环境变量
            "externalConsole": false,                         // true:输出到外部终端;false:只输出到软件终端(有显示不全的可能)
            "MIMode": "gdb",
            "setupCommands": [      // 暂时不知道作用
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",   // 预编译任务名称,和tasks.json中的label必须相同
            "miDebuggerPath": "/usr/bin/gdb"                   // 调试gdb路径
        }
    ]

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g","-std=c++11", 
                //"${file}",                            // 单个cpp文件
                "${workspaceFolder}/Demo/*.cpp",        // 多个cpp文件
                "${workspaceFolder}/src/*.cpp",
                "-o", 
                "${fileDirname}/${fileBasenameNoExtension}",  // 要调试的程序,必须与launch.json文件中的"program"相同
                "-I", "${workspaceFolder}/include",     // 项目include文件
                "-I", "$/usr/include/opencv4",          // opencv安装的include文件路径
                "-I", "$/usr/include/opencv4/opencv2",  
                "-L", "$/usr/local/lib",                //opencv安装的lib文件路径
                "-l", "opencv_core",                    //.so文件,文件原名称“libopencv_core.so”,注意需去除“lib”
                "-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",    
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "detail": "调试器生成任务"
        }
    ],
    "version": "2.0.0"

第二个版本
  强烈推荐使用此方法,这个方法除了配置opencv第三方库,还可以配置其他第三方库,根据需求可自行添加。这方法的原理是根据第三方库的 pkg-config 来配置的(即后缀为 .pc 的文件),然后系统会根据这个文件去自动寻找并链接include路径以及lib库。所以在根据我上面的教程来安装opencv时,一定不要忘记配置 pkg-config 环境了。下面将贴出三个配置代码

c_c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/x86_64-linux-gnu/sys",
                "/usr/local/include/opencv4",//记得把opencv4的include路径包含,按我上述方法来安装opencv的话一般就是这个路径
                "/usr/include/x86_64-linux-gnu/sys"//这是装了cuda的路径,如果无cuda请去除
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

{
    "configurations": [
        {
            "name": "C/C++: cpp 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [
                "`pkg-config","--libs","--cflags","opencv4`",
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",//要和 task.json的label字段一致
            "miDebuggerPath": "/usr/bin/gdb",
            //下面这行命令可以去除Vscode中编译后出现 [1]+   Done....  的提示
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi"
        }
    ],
    "version": "2.0.0"
}

task.json
  先用命令窗口尝试命令pkg-config --libs --cflags opencv4,看看你能不能获得第三方库的信息如下图,如果可以的话就代表你这个第三方库是有 .pc 文件的
在这里插入图片描述

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            //"command": "/usr/bin/cpp",                  //不能用这命名, 否则你可能会i报错 gdb failed with message 'file format not recognized'
            "command": "/usr/bin/g++", 
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "`pkg-config","--libs","--cflags","opencv4`"//这是添加第三方库代码,要添加什么库直接继续加就行,不要忘记了 ` 符号
                //如下面再加个 gazebo11 第三方库(你得保证你安装的第三方库中含有.pc文件)
              // "`pkg-config","--libs","--cflags","opencv4,gazebo11`"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

至此我们已经配置好了,那么可以来测试下啦(如下图,自己加上test.jpg)
在这里插入图片描述
贴出测试代码

#include
#include
#include
#include 
using namespace std;
using namespace cv;
int main()
{
    Mat srcImage = Mat::zeros(Size(200,200), CV_8UC1);
    imshow("Origin",srcImage);
    waitKey(0);
    return 0;
}

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