vscode C++ debug

这是一个关于如何在Visual Studio Code(VSCode)中配置C++的编译和调试环境的配置示例。`launch.json`文件设置了使用g++.exe编译器进行调试,而`task.json`文件定义了编译任务,包括命令、参数和输出文件路径。通过这两个配置文件,开发者可以实现C++源代码的快速构建和调试。
摘要由CSDN通过智能技术生成

// launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "g++.exe - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ],
        "preLaunchTask": "C/C++: g++.exe build active file"
      }
    ]
  }

// task.json

{
    "tasks": [
      {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:/MinGW/bin/g++.exe",
        "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
        "options": {
          "cwd": "${fileDirname}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "detail": "compiler: C:/MinGW/bin/g++.exe"
      }
    ],
    "version": "2.0.0"
  }
### 配置 VSCode 进行 C++ 调试 #### 安装必要的工具和扩展 为了能够在 Linux 下通过 Visual Studio Code (VSCode) 编辑器来编写、编译并调试 C++ 程序,需先确保已经安装了 GCC 或 Clang 编译器以及 GDB 调试器。此外还需要在 VSCode 中安装 "C/C++" 插件[^1]。 #### 创建项目结构 创建一个新的工作区目录用于存放源码文件和其他配置文件。对于简单的单文件程序可以直接在这个根目录下放置 .cpp 文件;而对于多文件或多模块的大型项目,则建议采用更复杂的组织方式,并考虑使用构建系统如 Makefile 或者 CMake 来管理依赖关系。 #### 设置任务定义(task.json) 编辑 `.vscode/tasks.json` 文件以指定如何调用外部命令来进行项目的编译过程。下面是一个针对单一 cpp 文件的例子: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "/usr/bin/g++", "args": [ "-g", "${workspaceFolder}/main.cpp", "-o", "${workspaceFolder}/a.out" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task to build a simple c++ program." } ] } ``` 此脚本将会把 `main.cpp` 编译成名为 `a.out` 的二进制文件[^2]。 #### 构建启动配置(launch.json) 接下来要做的就是告诉 VSCode 如何启动调试会话。这可以通过修改位于 `.vscode/launch.json` 的 JSON 文档实现: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello world", "miDebuggerPath": "/usr/bin/gdb", "logging": {"trace":true,"traceResponse":true}, "internalConsoleOptions": "openOnSessionStart" } ] } ``` 这段配置指定了当点击“开始调试”按钮时应该做什么动作——即先执行之前定义的任务去编译代码,再利用 GDB 对生成的目标文件进行调试。 #### 解决第三方库路径问题 如果项目中包含了来自特定位置(比如 `/usr/local/include/opencv4/opencv2/core/core.hpp`)的头文件,在包含这些头文件的时候需要注意其相对路径是否正确。例如 OpenCV 库可能需要特别处理才能正常链接和导入,确保按照实际安装情况调整 include 语句[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值