3

When trying to debug c++ with VSCODE, I get the following error:

> 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 Answers 3

11

It seems you make a mistake. You are actually trying to compile the JSON file other than the cpp file that you are working on. You should first switch to the tab of the cpp code and then run task or run build task.

Sign up to request clarification or add additional context in comments.

Comments

3

Found my problem:

  • These files(launch.json and tasks.json) were generated by VSCode for me.
  • However, delete this line: "preLaunchTask": "g++ build active file",
  • Also modify this line: "program":"${workspaceRoot}/TcpSocketClassTester/TcpSocketClassTester",
  • And finally delete the file: tasks.json (not needed)

Comments

2

As answered by darcy it is trying to compile the wrong file.

In your tasks.json you can see in the args array this line: "${file}", what it does is to fill the full path of the file you are trying to compile.

The value for this line changes for the document on focus on your vscode. Therefore it is safe to assume you are trying to run the task while having the task.json file on focus, just change it to the .cpp file you want to compile, hit F5 or click the play button in the debug tab and it should work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.