使用调试器时出现Vscode错误

3

在尝试使用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"
}
3个回答

12

看起来你犯了一个错误。实际上你试图编译的是与你正在工作的cpp文件不同的JSON文件。你应该先切换到cpp代码的选项卡,然后再运行run task或者run build task


3

找到了我的问题:

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

2
根据Darcy的回答,它试图编译错误的文件。
在您的tasks.json中,您可以在args数组中看到这一行:"${file}",它会填充您想要编译的文件的完整路径。
此行的值因当前焦点文档而异。因此,可以安全地假设您正在尝试运行任务时,在vscode上有task.json文件处于焦点状态。只需将其更改为您想要编译的.cpp文件,按F5或单击调试选项卡中的播放按钮即可正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接