【问题标题】:Vscode errors using the debugger使用调试器的 Vscode 错误
【发布时间】:2019-07-08 15:23:29
【问题描述】:

尝试使用 VSCODE 调试 c++ 时,出现以下错误:

> Executing task: /usr/bin/g++ -g /home/sa/Genesis/Dev/TcpSocketClassTester/.vscode/launch.json -o /home/sa/Genesis/Dev/TcpSocketClassTester/.vscode/launch <

/usr/bin/ld:/home/sa/Genesis/Dev/TcpSocketClassTester/.vscode/launch.json: file format not recognized; treating as linker script
/usr/bin/ld:/home/sa/Genesis/Dev/TcpSocketClassTester/.vscode/launch.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

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": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ],
    "version": "2.0.0"
}

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    看来你搞错了。您实际上是在尝试编译 JSON 文件而不是您正在处理的 cpp 文件。您应该先切换到 cpp 代码的选项卡,然后再切换到 run task 或 run build task。

    【讨论】:

      【解决方案2】:

      发现我的问题:

      • 这些文件(launch.json 和 tasks.json)是 VSCode 为我生成的。
      • 但是,删除这一行:"preLaunchTask": "g++ build active file",
      • 同样修改这一行:"program":"${workspaceRoot}/TcpSocketClassTester/TcpSocketClassTester",
      • 最后删除文件:tasks.json(不需要)

      【讨论】:

        【解决方案3】:

        正如 darcy 所回答的,它试图编译错误的文件。

        在您的 tasks.json 中,您可以在 args 数组中看到这一行:“${file}”,它的作用是填充您尝试编译的文件的完整路径。

        此行的值会随着文档的焦点在您的 vscode 上而改变。因此可以安全地假设您正在尝试运行任务,同时让 task.json 文件成为焦点,只需将其更改为您要编译的 .cpp 文件,按 F5 或单击调试选项卡中的播放按钮,它应该工作正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-12
          • 2019-05-06
          • 2021-07-23
          • 1970-01-01
          • 2021-03-15
          • 2020-02-24
          • 2017-04-28
          • 1970-01-01
          相关资源
          最近更新 更多