VS 代码C++:无法启动调试 - 程序路径丢失或无效

C++ for VS code: Unable to start debugging - Program path is missing or invalid

本文关键字:路径 程序 无效 调试 C++ 代码 启动 VS      更新时间:2023-10-16

无法启动调试。程序路径"/home/student/Documents/Visual Studio Code/rectangle"缺失或无效。

我的launch.json看起来像这样:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "launchOptionType": "Local",
            "miDebuggerPath": "/usr/bin/gdb",
            "targetArchitecture": "x64",
            "program": "${workspaceRoot}/rectangle",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": []
        },
        {
            "name": "C++ Attach (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "launchOptionType": "Local",
            "miDebuggerPath": "/usr/bin/gdb",
            "targetArchitecture": "x64",
            "program": "${workspaceRoot}/rectangle",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": []
        }
    ]
}

我C++程序是这样的:

#include <iostream>
using namespace std;
int main()
{
    double length, width, area;
    cout << "Enter the length: ";
    cin >> length;
    cout << "Enter the width: ";
    cin >> width;
    area = length * width;
    cout << "The area is " << area << endl;
    return 0;
}

"矩形"文件是上面提到的C++源代码吗?

如果是这样,按照惯例,它应该重命名为"矩形.cpp",然后编译成二进制/可运行程序 - 可以命名为"矩形"。

据我所知,您必须使用 VSCODE 外部的编译器,但可以设置一个构建任务(如果您感觉高级,还可以设置文件观察器和问题匹配器)来自动执行编译过程。

您是同时安装 Cygwin

和 minGW 还是只是安装 Cygwin?
在以上两种情况下,请确保VSCODE调用minGW的g++和gdb,您只需添加minGW的bin路径并从系统环境中删除Cygwin的bin路径即可。因为 Cygwin 构建的 exe 依赖于 cygwin1.dll这不是纯 win exe,所以 gdb 不能很好地处理这个 exe。