windows&ubuntu22.04中vscode调试运行

文章描述了如何在Windows和Ubuntu22.04系统中更新launch.json和tasks.json文件,以适应不同的C++项目编译和调试需求,包括修改编译命令、设置正确的编译器路径以及调试配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows运行

launch.json中改为

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

tasks.json中改为

注意修改编译器的路径


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++", //这里注意一下,见下文
            "command": "D:\\Program Files\\x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\Program Files\\x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

ubuntu22.04运行

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": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /usr/bin/gdb -q --interpreter=mi",
            "setupCommands": [
                {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
                }
            ]
        }
        ]
}

tasks.json中修改为

{
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-std=c++11",
                "-o",
                "${fileBasenameNoExtension}.out"
            ]
        }
    ],
    "version": "2.0.0"
}

### 安装 Visual Studio Code 为了在 Ubuntu 22.04 上成功安装 Visual Studio Code (VS Code),需遵循一系列特定指令来确保软件能顺利运行。 #### 下载并安装 VS Code 通过官方渠道获取最新版的 Visual Studio Code 是推荐的做法。打开终端,输入命令更新本地包索引: ```bash sudo apt update ``` 之后,可以通过两种方式之一来进行安装:一种是从 Microsoft 的官方网站下载 `.deb` 文件,另一种是添加 Microsoft 的 APT 存储库以便于后续自动更新[^1]。 对于后者,在终端执行如下命令序列以完成安装过程: ```bash wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list' rm -f packages.microsoft.gpg sudo apt install apt-transport-https sudo apt update sudo apt install code # or code-insiders ``` 上述脚本会先导入 GPG 密钥,并设置好对应的APT源地址,最后再利用 `apt-get` 工具实际安装Visual Studio Code应用本身[^3]。 一旦安装完毕,可通过启动器或者直接在命令行键入 `code .` 来开启编辑器及其所在目录作为工作区。 #### 配置 C++ 开发环境 针对C++编程的支持,则需要额外安装一些必要的工具链组件,包括但不限于编译器(gcc/g++)、调试器(GDB)以及 IntelliSense 所依赖的语言服务器(Clang)。 这一步骤同样可以在终端里快速达成目标: ```bash sudo apt install build-essential gdb clang ``` 以上命令将部署 GCC 编译套件和其他辅助程序,从而让开发者能够在 VS Code 中编写、构建和测试 C++ 应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值