在 Windows 上使用 Cygwin64 编译器和调试器为 C 设置 VS Code(错误:无法启动调试)

Leo*_*iro 5 c debugging cygwin visual-studio-code

我正在尝试设置 VSCODE 以使用 Cygwin64 在 Windows 上调试C 程序。

我使用了 @stephw 建议的配置(在 Windows 上使用 Cygwin64 编译器和调试器设置 C 的 VS 代码),但它对我不起作用。

我无法评论原始帖子,因为我没有足够的声誉点,并且我无法回答,因为我不知道原始问题的答案。

脚本的名称是dirigir.c,我可以编译。创建文件 dirigir.exe。但...

我收到以下错误:

错误:无法开始调试。命令“-exec-run”出现意外的 GDB 输出。创建进程 /usr/bin/E:\cloud\Backup\Trabalhos com 程序\C 和 Cpp\scripts/e:\cloud\Backup\Trabalhos com 程序\C 和 Cpp\scripts\dirigir.exe 时出错,(错误 2) 。

由于某种原因,/usr/bin/ ... /被插入到路径中,并且 .exe 的路径是重复的

我的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": "gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "%PATH%;z:\\cygwin64\\bin"
                }
            ],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "logging": { "engineLogging": true }, //optional
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的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": "gcc.exe build active file",
            "command": "C:\\cygwin64\\bin\\gcc.exe",
            "args": [
                "-g",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-Werror", // Optional
                "-Wall", // Optional
                "-Wextra", // Optional
                "-ansi", // Optional
                "-pedantic", // Optional
                "${file}"
            ],
            "options": {
                "cwd": "C:\\cygwin64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

您知道如何继续吗?先感谢您。

Leo*_*iro 4

最后,我成功了。

\n

基于 WardenGnaw 在这个线程中的答案:\n [cppdbg] Cygwin 10.1-1: intereratedTerminal无法接受输入#6475,当然,@stephw\在这个问题所依据的原始问题中的答案,我可以调试我的 C 程序。

\n

首先,我在C 程序所在的同一文件夹中保存了 VSCODE工作区。

\n

然后,我使用了最新的Cygwin gdb版本(10.2-1)。不起作用(我收到了提出这个问题的错误消息)。然后,我尝试了9.2-1,现在它可以工作了。

\n

Cygwin 安装窗口和当前安装的 gdb 版本。

\n

请务必记住将“C:\\cygwin64\\bin”添加到 PATH。

\n

环境变量窗口

\n

我改变了launch.json一点点。请注意,“preLaunchTask”键的值与“label”键的值不完全相同tasks.json

\n
{\n    // Use IntelliSense to learn about possible attributes.\n    // Hover to view descriptions of existing attributes.\n    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387\n    "version": "0.2.0",\n    "configurations": [\n        {\n            "name": "gcc.exe build and debug active file",\n            "type": "cppdbg",\n            "request": "launch",\n            "program": "${fileDirname}\\\\${fileBasenameNoExtension}.exe",\n            "args": [],\n            "stopAtEntry": false,\n            "cwd": "${workspaceFolder}",\n            "environment": [\n                {\n                    "name": "PATH",\n                    "value": "%PATH%;C:\\\\cygwin64\\\\bin"\n                }\n            ],\n            "externalConsole": false,\n            "MIMode": "gdb",\n            "miDebuggerPath": "C:\\\\cygwin64\\\\bin\\\\gdb.exe",\n            "setupCommands": [\n                {\n                    "description": "Enable pretty-printing for gdb",\n                    "text": "-enable-pretty-printing",\n                    "ignoreFailures": true\n                }\n            ],\n            "logging": { "engineLogging": true }, //optional\n            "preLaunchTask": "gcc.exe build active file"\n        }\n    ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的tasks.json是这个(注意最后我的名字,在“详细信息”键中):

\n
{\n    "version": "2.0.0",\n    "tasks": [\n        {\n            "type": "cppbuild",\n            "label": "C/C++: gcc.exe build active file",\n            "command": "C:\\\\cygwin64\\\\bin\\\\gcc.exe",\n            "args": [\n                "-fdiagnostics-color=always",\n                "-g",\n                "${file}",\n                "-o",\n                "${fileDirname}\\\\${fileBasenameNoExtension}.exe"\n            ],\n            "options": {\n                "cwd": "${fileDirname}"\n            },\n            "problemMatcher": [\n                "$gcc"\n            ],\n            "group": "build",\n            "detail": "Task generated by Leonardo."\n        }\n    ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我注意到有些奇怪的事情:\n当我按 F5 时,它不起作用,因为找不到创建的任务。

\n

对话框显示未找到任务。

\n

当我将launch.json文件(preLaunchTask 键)编辑为文件中完全相同的值时tasks.json,“未找到”错误消失,但调试过程不会启动。

\n
"preLaunchTask": "C/C++: gcc.exe build active file"\n
Run Code Online (Sandbox Code Playgroud)\n

如果我按“运行并调试播放”按钮,调试过程也不起作用。

\n

Visual Studio Code 运行和调试播放按钮。

\n

当我使用命令面板(按 CTRL+SHIFT+P)并单击 时C/C++: Build and Debug Active File

\n

Visual Studio Code 命令面板...

\n

然后选择我创建的任务(第二个任务似乎是自动的),\n可供执行的任务列表。

\n

程序已编译并开始调试过程。

\n

这是我非常简单的测试脚本:

\n
#include <stdio.h>\n#include <stdlib.h>\n\nint main() {\n\n    /* declare and initialize string */\n    char myString[] = "This is an example of string declaration!";\n\n    /* print string */\n    printf("%s", myString);\n\n    int numberA = 5;\n    int numberB = 10;\n\n    int sum = numberA + numberB;\n\n    /* Printing a number */\n    printf("A soma de numberA + numberB \xc3\xa9: %d", sum);\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我希望这个问题和答案可以帮助将来的人。和平。

\n