vscode配置

插件安装

  • c/c++
  • cmake
  • python
    禁用cmake tool保留cmake的语法高亮和提示的插件,cmake tool默认在build文件夹下进行编译,这里禁用该插件,通过tasks.json文件进行配置

配置文件作用

tasks.json
简单理解为定义一些脚本工具,可以在menu-terminal-run task中找到定义的脚本
c_cpp_properties.json
配置cpp查找目录,宏定义等,便于智能提示及跳转
settings.json
配置vscode
launch.json
用于调试及运行(暂时没完全理解,后续补充)

典型settings.json文件

{
    // settings for cpp
    "files.associations": {
        "iostream": "cpp",
        "xstring": "cpp",
        "vector": "cpp"
    },

    // settings for python
    "python.autoComplete.extraPaths": [
        "/opt/ros/melodic/lib/python2.7/dist-packages",
    ],
    "python.analysis.extraPaths": [
        "/opt/ros/melodic/lib/python2.7/dist-packages",
    ],

    // global settings

    // update vscode manually
    "update.mode": "manual",

    // auto save 
    "files.autoSave": "afterDelay",
}

配置cpp

tasks.json

{
    "version": "2.0.0",
    "tasks": [{
            "label": "compile",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/build/${fileBasenameNoExtension}"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

上述配置等价于在命令行中执行:

/usr/bin/g++ -g ${file} -o ${fileDirname}/build/${fileBasenameNoExtension}

会将生成的文件保存在build文件夹下
其执行终端显示

Executing task: /usr/bin/g++ -g /home/zhao/vscode_pro/build_cpp/hello_world.cpp -o /home/zhao/vscode_pro/build_cpp/build/hello_world

注意:

  • 需要将要编译的文件置于激活状态(即当前窗口打开的是要编译的文件)
  • label对应的值便是该脚本名字

若要调试,则需要配置launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "compile",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

其中program指定要调试的文件,而preLaunchTask指定在运行该launch前需要执行的操作,这里定义了在调试之前先调用compile进行编译

配置cmake

为了便于项目管理,使用cmake,这里的配置方式就是将vscode视为一个编译器,其他的操作调用task定义的脚本完成
正常的cmake步骤为

mkdir build && cd build
cmake ..
make 

这里的tasks.json配置文件如下

{
    "options": {
       "cwd": "${workspaceFolder}/cmake-build-debug"
    },
    "tasks": [
       {
          "label": "cmake",
         //  "command":"cmake",
          "command":"/usr/local/bin/cmake",
          "args": ["-DCMAKE_BUILD_TYPE=Debug", ".."]
       },
       {
          "label": "make",
          "command":"make",
          "args": ["-j9", ]
       },
       {
          "label": "CMake Build",
          "dependsOn":[
             "cmake",
             "make"
          ],
       }
    ],
    "version": "2.0.0"
 }

很好理解,注意指定的当前工作目录cwd,表示执行task脚本是在该目录下进行的,同理可以添加其他的cmake及make操作
也可以启用cmake tool利用定义好的一系列task操作,直接执行,不过默认是在build目录下构建的

配置python

python配置对应的查找目录及python路径,通过settings.json配置查找补全目录,通过配置task设置执行python的路径,既然tasks只是定义了一个脚本,则可以采用如下方式配置tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python_env_name",
            "type": "shell",
            // "command": "python",
            "command": "/home/zhao/anaconda3/envs/env_name/bin/python",
            "args": ["${file}"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
        }
    ]
}

配置ros

同理仅仅将vscode当作编辑器即可,直接配置task的一个执行脚本

{
    "options": {
       "cwd": "${workspaceFolder}/.."
    },
    "tasks": [
       {
          "label": "catkin_make",
          "command":"catkin_make",
          "args": []
       },
    ],
    "version": "2.0.0"
 }

注意这里的cwd设置,可见我打开的根目录为ros工作空间的src目录
为了使得可以跳转到生成的msg和srv头文件,在c_cpp_properties.json中添加查找目录

{
    "configurations": [
        {
            "name": "ROS",
            "includePath": [
                "/opt/ros/melodic/include/**",
                "/usr/include/**",
                "/usr/local/include/**",
                "${workspaceFolder}/**",
                "${workspaceFolder}/../devel/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

你可能感兴趣的:(vscode)