Debian | Vscode 安装与配置 C 环境

Debian | Vscode 安装与配置 C 环境

安装 vscode

shell 复制代码
sudo apt update
sudo apt install software-properties-common apt-transport-https curl

curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"

sudo apt update
sudo apt install code

配置环境

shell 复制代码
sudo apt install build-essential gdb

安装插件

下载插件 C/C++

创建一个 cpp 文件,写入 c++ 代码,并进行 Debug C/C++ File

cpp 复制代码
#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout << "hello" << endl;
    return 0;
}

选择 g++

此时在 .vscode 文件夹下会多出 tasks.json

json 复制代码
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

同时,终端输出 hello

按 Ctrl + Shift + P 在上方输入 configuration

找到 C/C++: Edit Configurations(JSON) 并点击

此时在 .vscode 目录下面会多出 c_cpp_properties.json

json 复制代码
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

点击左侧 Run and Debug 图标:

点击 create a launch.json file

选择 C++ (GDB/LLDB)

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": [
 
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb",
        }
    ]
}

解决终端出现 [1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-wyyxzchw.1pu" 1>"/tmp/Microsoft-MIEngine-Out-qddpshqk.bcg"

在 launch.json 的 configuration 中添加:

json 复制代码
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi",

重新编译运行

最终,launch.json 内容为

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": [
 
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb",
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi",
        }
    ]
}
相关推荐
阿巴~阿巴~2 小时前
C_深入理解指针(五) —— sizeof和strlen的对比、数组和指针笔试题解析、指针运算笔试题解析
c语言·开发语言·数据结构·算法
八月的雨季 最後的冰吻6 小时前
C--字符串函数处理总结
c语言·前端·算法
Mr_Xuhhh7 小时前
C语言深度剖析--不定期更新的第六弹
c语言·开发语言·数据结构·算法
善 .8 小时前
C语言编译过程
c语言·开发语言
黄卷青灯778 小时前
c语言 #define 详解
c语言·开发语言·define
2023王先生要加油鸭9 小时前
81.C语言中的内存分布
c语言·开发语言
北航最菜大学生9 小时前
数据结构(五)----树(含堆)
c语言·数据结构
pzn250610 小时前
排序进阶(C语言)
c语言·数据结构
什么都没学会11 小时前
GO环境安装
开发语言·vscode·golang·安装
宝贝儿好11 小时前
【C++】作用域指针、智能指针、共享指针、弱指针
java·c语言·jvm·c++