【问题标题】:How to debug C++ program in VSCode with file added如何在 VSCode 中调试 C++ 程序并添加文件
【发布时间】:2020-09-27 03:08:58
【问题描述】:

我可以在不添加文件的情​​况下进行调试,但是,我不知道如何处理添加文件。

命令如下:

g++ -g --std=c++11 lab1.cpp -o lab1

./lab1 ./tests/extracredit-test-0.txt

以下是我的task.jsonlaunch.json。我想知道如何修改它以支持添加文件的调试。

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": [
    {
      "preLaunchTask": "build active file",
      "name": "CodeLLDB",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
    },
    {
      "preLaunchTask": "build active file",
      "name": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb"
    },
    {
      "name": "GDB",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "cwd": "${workspaceFolder}",

      "MIMode": "gdb"

  }
  ]
}

task.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build active file",
      "type": "shell",
      "command": "g++ --std=c++11 ${file} -g -o  ${fileDirname}/${fileBasenameNoExtension}",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared"
      },
      "osx": {
        "command": "g++ --std=c++11 ${file} -g -o  ${fileDirname}/${fileBasenameNoExtension}",
      },
      "windows": {
        "command": "g++",
        "args": [
          "-ggdb",
          "\"${file}\"",
          "--std=c++11",
          "-o",
          "\"${fileDirname}\\${fileBasenameNoExtension}\""
        ]
      }
    }
  ]
}

【问题讨论】:

    标签: c++ debugging gcc visual-studio-code


    【解决方案1】:

    如何在 VSCode 中调试 C++ 程序并添加文件

    您正在使用GCC。考虑升级到GCC 10。然后编译withg++ -Wall -Wextra -g(所有警告和DWARF调试信息)并使用GDB调试器。您可能会对 GCC 10 中引入的 static analysis facilities 和/或 Frama-CClang Static Analyzer 等工具感兴趣。

    阅读 GCC、VSCode 和您最喜欢的调试器的文档。

    另请参阅this draft 报告。

    如果您编译一个大型软件(例如一百万行 C++),您可能还会对通过您的 build automation 工具(例如如ninjaGNU make)。在某些情况下,您可能会对某些 C++ 代码生成器感兴趣,例如 SWIGANTLR。在某些项目中,您会编写自己的 C++ 代码生成器(例如,使用 GuileGPPGNU gawkPythonm4)。请注意GCC 在内部使用几个 C++ 代码生成器,并接受plugins

    在几个开源项目的源代码中寻找灵感,例如QtWtClanglibbacktraceFLTK。您也可以通过LinuxFromScratch 感兴趣。

    【讨论】:

      【解决方案2】:

      这很简单,在这个sn-p的代码中

      "configurations": [
          {
            "preLaunchTask": "build active file",
            "name": "CodeLLDB",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
          },
      

      只需在args中添加network.txt之类的文件,在这种情况下,就像

      "args": ["network.txt"]
      

      这就完成了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        • 1970-01-01
        • 2021-03-16
        相关资源
        最近更新 更多