vscode的VI
vscode的VI

首先安装好VSCode的有关于C++开发的有关插件:
- C/C++
- C++ Intellisense

同时确保你的电脑上有C++编译器和调试器:
- Mingw / g++
- GDB(调试器)
……

在首次按下F5开始调试之后,会在.vscode文件夹下面自动生成
- launch.json文件
- c_cpp_properties.json文件
- tasks.json文件

launch.json文件是vs调试器的配置文件,包括了当前项目的调试任务的name,所使用的调试器等信息,同时也指定了调试任务开始之前的编译过程(preLaunchTask);
c_cpp_properties.json文件中写明了当前代码提示的有关include的路径以及所使用的编译器和代码提示模式和当前使用的C++和C语言标准的版本(i.e. c++11/c11)等信息;
另一个配置文件就是tasks.json,里面配置了当前工作区里面的所有任务(你可以把一个个编译调试过程分别理解成一个个任务),在这里你可以定义具体每一个任务的情况:
- type指定任务运行的环境,假使你要编译一个cpp文件,type指定为shell类型
- label指的是当前任务的名称,这个名称可以被其他的任务调用
- command指的是执行当前任务的程序如果是编译的话,一般是g++或是gcc
- Args指的是执行这一个任务的时候所需要执行的所有参数,这里发现一个比较坑的地方,这里不能使用命令行中的pkg-config命令,因此需要使用opencv的同学需要一行一行的自己添加依赖库了
- 最后有一个options选项,这一项貌似没有什么具体的关于编译调试需要进行的配置,因此这里可以先不管它

综上就是在VSCode中编译调试C++代码的过程了,虽然不如Visual Studio方便快捷,但谁让人家是宇宙第一IDE呢还不支持Linux
下面贴上我自己的这三个文件的配置demo:

/*c_cpp_properties.json:*/
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/",
                "/usr/include/",
                "/usr/local/include/",
                "/usr/lib/",
                "/usr/local/lib/**",
                "/usr/local/include/opencv2"
            ],
            "defines": [
                "_DEBUG"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64",
            "browse": {}
        }
    ],
    "version": 4
}
/* tasks.json: */
{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ get dataset",
            "command": "/usr/bin/g++",
            "args": [
                "./getDataSet.cpp",
                "-I",
                "/usr/local/include/opencv", 
                "-I",
                "/usr/local/include", 
                "-L",
                "/usr/local/lib", 
                "-l",
                "opencv_calib3d", 
                "-l",
                "opencv_contrib", 
                "-l",
                "opencv_core",
                "-l",
                "opencv_features2d", 
                "-l",
                "opencv_flann",
                "-l",
                "opencv_highgui", 
                "-l",
                "opencv_imgproc", 
                "-l",
                "opencv_legacy" ,
                "-l",
                "opencv_ml", 
                "-l",
                "opencv_nonfree", 
                "-l",
                "opencv_objdetect",
                "-l",
                "opencv_ocl", 
                "-l",
                "opencv_photo",
                "-l",
                "opencv_stitching", 
                "-l",
                "opencv_superres", 
                "-l",
                "opencv_video", 
                "-l",
                "opencv_videostab", 
                "-l",
                "rt",
                "-l",
                "pthread",
                "-l",
                "m",
                "-l",
                "dl",
                "-o",
                "./getDataSet.o"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            }
        },
        {
            "type": "shell",
            "label": "g++ debug active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "main.cpp",
                "ClassPoseDetection.cpp",
                "-I",
                "/usr/local/include/opencv", 
                "-I",
                "/usr/local/include", 
                "-L",
                "/usr/local/lib", 
                "-l",
                "opencv_calib3d", 
                "-l",
                "opencv_contrib", 
                "-l",
                "opencv_core",
                "-l",
                "opencv_features2d", 
                "-l",
                "opencv_flann",
                "-l",
                "opencv_highgui", 
                "-l",
                "opencv_imgproc", 
                "-l",
                "opencv_legacy" ,
                "-l",
                "opencv_ml", 
                "-l",
                "opencv_nonfree", 
                "-l",
                "opencv_objdetect",
                "-l",
                "opencv_ocl", 
                "-l",
                "opencv_photo",
                "-l",
                "opencv_stitching", 
                "-l",
                "opencv_superres", 
                "-l",
                "opencv_video", 
                "-l",
                "opencv_videostab", 
                "-l",
                "rt",
                "-l",
                "pthread",
                "-l",
                "m",
                "-l",
                "dl",
                "-l",
                "MNN",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            }
        }
    ],
    "version": "2.0.0"
}
/* launch.json: */
{
    /* 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问:
    // https://go.microsoft.com/fwlink/?linkid=830387
    */
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ debug active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

最后修改日期:2019年10月24日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。