【问题标题】:Configuring launch.json, task.json and settings.json for debugging in VS Code with git bash as default terminal?使用 git bash 作为默认终端在 VS Code 中配置 launch.json、task.json 和 settings.json 以进行调试?
【发布时间】:2020-07-07 12:11:14
【问题描述】:

我正在尝试在 VSCode 中配置调试器

我查看了official documentation 来设置 C/C++ 的 VSCode 调试器,但它不起作用。

文档说明了设置 vscode 调试器的步骤 powershell 在 Windows 中。

但我正在尝试设置 debugger,并使用 git bash 作为我在 windows 中的默认 集成终端

我已将 git bash 添加为默认终端,但我无法将 debugger 与 git bash 设置为集成 terminal

默认配置文件

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": "${workspaceFolder}",
            "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"
        }
    ]
}

属性

"externalConsole": false

设置为 false 因为我希望 VSCode 使用集成的默认 bash 终端,而不是使用外部终端进行调试。

tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

settings.json

{
    "files.associations": {
        "iostream": "cpp"
    },
    "C_Cpp.errorSquiggles": "Disabled"
}

使用上述配置,当我开始调试时,它给了我以下错误:

tasks.json 中的 command 属性似乎不正确, 作为 bash 转换

"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"

并给出错误:“找不到命令”,因为 bash 中的反斜杠('\')是一个转义字符

现在将 tasks.json 中的上述路径更改为 bash 样式:

"command": "C:/MinGW/bin/g++.exe"

解决了上述错误,但现在它对变量 ${file} 给出了相同的错误,因为此路径变量被动态设置为当前打开的文件 .cpp。 p>

过去几天我一直在处理这个调试问题,但还没有找到任何解决方法。

如何通过配置文件更改/更新以在VSCode中使用git bash作为默认集成终端调试

【问题讨论】:

    标签: visual-studio-code vscode-settings git-bash vscode-debugger vscode-tasks


    【解决方案1】:

    tasks.json 中的 command 属性似乎不正确,因为 bash 转换了

    "C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
    

    然后尝试类似cygwin的路径:

    /c/MingW/bin/g++.exe
    # or
    /C/MingW/bin/g++.exe
    

    然后检查它是否被 git bash 会话正确解释。

    【讨论】:

    • 是的,它肯定会解决编译器路径错误,但它会为 tasks.json 中的路径变量 ${file} 提供相同的错误,因为此路径变量也使用反斜杠来分隔目录。更新错误。
    • @tusharRawat 我想知道 WSL 是否比 cygwin 更适合 (github.com/microsoft/vscode-cpptools/issues/…)
    • 是的 WSL 可能会工作,但只是为了使用 linux 终端的体验(通过 git bash),我不喜欢为 windows 安装完整的 WSL。必须有一些解决方法才能在 Windows 中使用 git bash 作为终端。
    猜你喜欢
    • 2021-12-07
    • 2018-05-07
    • 1970-01-01
    • 2021-05-12
    • 2021-08-17
    • 2018-07-19
    • 1970-01-01
    • 2021-11-01
    • 2022-10-25
    相关资源
    最近更新 更多