ubuntu18.04 配置vscode的C++/C环境+opencv

目录

 

一、安装vscode

二、安装C/C++插件

三、配置调试和编译文件

3.1 tasks.json文件----生成out文件

3.2 launch.json文件----执行out文件

四 执行

 


一、安装vscode

我使用anaconda安装的,可以参考vscode的主页,有安装方式。

二、安装C/C++插件

按Ctrl + shift + X,搜索C/C++并安装。安装它就行。

三、配置调试和编译文件

3.1 tasks.json文件----生成out文件

为了方便在VScode里编译C++代码,我们可以将类似g++ -g main.cpp等g++命令写入VScode的任务系统。

按ctrl+shift+p打开命令行,输入Tasks: Run task,回车!!!,会出现如下提示:

    No task to run found. configure tasks...

再回车,

    Create tasks.json file from template

然后选择:

   Others Example to run an arbitrary external command.

生成默认的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", "${file}", "-o", "${fileBasenameNoExtension}.out"]
        }
    ]
}

这里的label为任务名,我们将”label"= "echo"改为”label"= "build"。
由于我们的指令是g++,这里将”command“=”echo Hello“改为”command“=”g++“。
然后添加g++的参数args。如果我们的g++指令为:g++ -g main.cpp,这里可以把参数设置为如下:

3.2 launch.json文件----执行out文件

选中文件夹/cpp文件,按Ctrl+shift+D,点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C++(GDB/LLDB)回车!!将自动生成launch.json文件,修改内容如下:

            "program": "enter program name, for example ${workspaceFolder}/a.out",

改为

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",

{
    // 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}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask":"build",   
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

"preLaunchTask":"build",这个参数必须有,否则会报错,在debug的时候,说lauch:file.out文件不存在。

四 执行

F5

五 OPENCV编译环境

5.1 配置opencv

去这里 https://github.com/opencv/opencv/releases  下载,tar.tg文件。加压。

cd opencv/cmake 
cmake ..
suido make
sudo make install

5.2 配置vscode环境

5.2.1 c_cpp_properties.json

按ctrl+shift+p,输入edit,选择C/C++:edit configuration(json),会弹出c_cpp_properties.json。他是配置外部环境的。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
修改它
"includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include", //请确保你的opencv opencv2头文件夹安装在这个目录

5.2.2 task.josn

tasks.json里面修改的是参数问题,我还没试试那个好使

"args": [
                "-g",
                "${workspaceRoot}/test.cpp",
                "-L", "/usr/local/opencv3.4/lib/lib*",
                "-o",
                "run"
                
            ],
"args": [
        "-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}.o",// 设置动态链接库
        "-I", "/usr/local/include",
        "-I", "/usr/local/include/opencv",
        "-I", "/usr/local/include/opencv2",
        "-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"
    ],// 编译命令参数

5.2.3  libjasper.so.1

缺少libjasper.so.1,有啥办法,安装呗。
命令如下:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

猜你喜欢

转载自blog.csdn.net/weixin_39875161/article/details/92005225