【笔记】vscode做C++开发,launch.json、tasks.json、settings.json写法示例

vscode的使用笔记

预定义变量的意义解释

${workspaceFolder} :表示当前workspace文件夹路径,如C:\Users\admin\Desktop\test

${workspaceRootFolderName}:表示workspace的文件夹名,如test

${file}:文件自身的绝对路径,如C:\Users\admin\Desktop\test\.vscode\launch.json

${relativeFile}:文件在workspace中的路径,如.vscode\launch.json

${fileBasenameNoExtension}:当前文件的文件名,不带后缀,如hello/launch

${fileBasename}:当前文件的文件名,如 hello.cpp/launch.json等

${fileDirname}:文件所在的文件夹路径,也即C:\Users\admin\Desktop\test\.vscode

${fileExtname}:当前文件的后缀,也即.json

${lineNumber}:当前文件光标所在的行号

${env:PATH}:系统中的环境变量

一、launch.json

在当前文件上是c++的情况下,tasks可以用来做编译,而launch用来执行编译好的文件

  1. 为了能够 debugging ,需要生成一个launch.json文件。linux下vscode支持的调试器为gdb

  2. launch文件实例(linux)

{
    // 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": "available launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/devel/lib/machine_control/machine_control",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  1. launch文件的写法及参数意义(preLauchTask可将launch.json和tasks.json文件关联起来,用于在调试前完成build)任务
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",              //  强制:就一个名字而已,但是是必须要有的
            "type": "cppdbg",                    //  强制:调试器的类型,Node debugger for  node, php for PHP , go for GO           
            "request": "launch",                 //  强制:launch/attach
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", // 可执行文件的路径,需修改
            "miDebuggerPath": "/usr/bin/gdb",    //  调试器的位置,
            "preLaunchTask":"build",             //  调试前编译任务名称
            "args": [],                          //  调试参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",         //  当前工作目录
            "environment": [],                   //  当前项目环境变量
            "externalConsole": true,
            "MIMode": "gdb",                     //  调试器模式/类型
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

二、tasks.json

  1. 要想从vscode中构建应用程序,必须要生成一个tasks.json文件。生成时,需要指定编译器

  2. tasks文件的写法及参数意义

{
    // 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",        // 编译任务的类型,通常为shell/process类型
            "command": "g++",       // 编译命令
            "args":[
                "-g",               // 该参数使编译器在编译的时候产生调试信息
                "${workspaceFolder}/${fileBasename}",    // 被编译文件,通常为.cpp/.c/.cc文件等
                "-I",                                    // include path指令
                "/usr/include",                          
                "-L",                                    // lib路径
                "/usr/lib/x86_64-linux-gnu",             
                "-l",                                    // 链接库文件1
                "opencv_core",                           
                "-l",                                    // 链接库文件2
                "opencv_highgui",                        
                "-o",                                    // 生成指定名称的可执行文件
                "${workspaceFolder}/${fileBasenameNoExtension}.out"  
          /* -g hello.cpp -I/usr/include -L/usr/lib/x86_64-linux-gnu -lopencv_core -o hello.out */
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "cmakebuild",      
            "type": "shell", 
            "command": "cd build && cmake ../ && make",    // shell 编译命令,做并运算,即前一命令执行失败,则后一命令也不执行
            "args": []
        }
    ] 

}

三、settings.json

  1. settings实例
{

  "C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools",

  "cmake.configureSettings": {"CMAKE_TOOLCHAIN_FILE": "toolchain.cmake",

  },

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值