Ubuntu22.04 vscode C++配置

program:调试入口文件的地址

cwd:程序启动调试的目录

miDebuggerPath:调试器的路径

1、首先安装g++环境。

键盘按下ctrl+alt+t打开控制台,输入

sudo apt-get install g++
  1. 打开VSCode,安装C++插件,安装C/C++和C/C++ Ru

Ubuntu22.04 vscode C++配置_第1张图片

launch.json

// {
//     // 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": []
// }

{
    // 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}/a.out",
            // "program": "${workspaceFolder}.out",
            // "program":  "${fileDirname}/${fileBasenameNoExtension}.out",//修改为.out
            // "program": "${fileDirname}\\output\\${fileBasenameNoExtension}.exe",
            // "program": "/home/kunsir/Documents/code/CProject/test/test/a.exe",
            // "program": "${workspaceRoot}/a.out", //${fileDirname}/${fileBasenameNoExtension}",
            "program": "${workspaceRoot}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "preLaunchTask": "C/C++: g++ 生成活动文件",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                // "${fileDirname}/${fileBasenameNoExtension}"
                "${workspaceRoot}/a.out",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

${workspaceRoot} 当前打开的文件夹的绝对路径+文件夹的名字

&{workspaceFolder}当前程序的路径,.vscode路径

${file}当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名

${fileBasename} 当前打开的文件名+后缀名,不包括路径

${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名

${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名

${fileExtname} 当前打开的文件的后缀名

你可能感兴趣的:(环境配置,vscode,C/C++)