【问题标题】:Debug for multiple C++ files in VSCode在 VSCode 中调试多个 C++ 文件
【发布时间】:2021-12-22 20:43:41
【问题描述】:

我认为我输入了所有正确的设置,但它给了我一个预启动任务错误,因为程序包含多个文件,所以我尝试更正 tasks.json 设置并将 args 设置为“*.cpp”,但我仍然有同样的问题。
请注意,所有文件都在同一个文件夹中,并且在编译过程中我没有错误。

预启动任务错误:

在 tasks.json 的 args 中设置“*.cpp”后,我仍然得到之前的错误 + 这个:

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, //set to true to see output in cmd instead
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\MinGW\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "g++.exe build active file"
            },
            {
                "name": "g++ build & run active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false, //set to true to see output in cmd instead
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\MinGW\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "g++ build & run active file"
            }
        ]
    }  

tasks.json 设置:

{
    "version": "2.0.0",
    
        "tasks": [
          {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
              "-g",
              "*.cpp",
              "-o",
              "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
              "cwd": "C:\\MinGW\\bin"
            }
          },
          {
            "type": "shell",
            "label": "g++ build & run active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
              "*.cpp",
              "-o",
              "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
              "cwd": "C:\\MinGW\\bin"
            }
          }
        ],
    
}  

【问题讨论】:

  • edit提出问题并添加按下Show Errors按钮时出现的文字。
  • @KenY-N 完成了,你有解决方案吗?
  • @VladfromMoscow 请问您知道解决方案吗?
  • 您的源文件必须在子目录中。
  • 这就是问题所在,您应该将测试目录中的 cpp 文件移动到其父文件夹,即您的项目文件夹,该文件夹应该是工作目录。或者你必须在执行 g++ cmd 之前 cd 到测试目录。

标签: c++ visual-studio-code vscode-debugger


【解决方案1】:

这是我的示例设置

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/api.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/",
            "environment": [],
            "preLaunchTask": "build",
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-Wall",
                "-o",
                "api.exe",
                "*.cpp"
            ]
        }
    ]
}

文件树应该是这样的

project           <- this is the working directory
    |--.vscode/
          |--*.json
    |--main.cpp
    |--other.cpp
    |--main.exe     <- this is the executable compiled by your task json

所以可执行路径应该是${workspaceFolder}/main.exe

【讨论】:

  • 启动:程序 'c:\Users ... \test\main.exe' 不存在
  • 但编译时没有错误
  • 如果编译时没有错误,则需要在windows文件资源管理器中验证可执行文件的位置。也许您的防病毒软件删除了您的可执行文件,或者您的问题可能与可执行文件路径中有空格有关。
  • "program": "${workspaceFolder}/main.exe",不是/test/main.exe
  • 我在 VS IDE 的多次迭代中都遇到了这个问题,因此在尝试解决问题时,如果用户名包含 unicode(尤其是如果那是奇怪的语言页面),我建议避免使用用户目录。 Studio 和 cl.exe 可能会折叠,如果用户名语言与其控制台区域设置不匹配,g++ 可能会折叠。
猜你喜欢
  • 2021-02-15
  • 2019-01-23
  • 1970-01-01
  • 2017-08-04
  • 2022-06-14
  • 2020-09-27
  • 2018-12-24
  • 2020-03-24
  • 2019-08-15
相关资源
最近更新 更多