Home > Net >  How to debug c file in vscode on macOS?
How to debug c file in vscode on macOS?

Time:09-20

I've been trying to debug .cpp file in vscode and following files have been installed perfectly. (I've ran .cpp file via code-runner extension but now I'd like to debug c files.)

$ clang --version // or g   --version (both same result)

Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

And I've clicked the debug button and tried every compiler that listed

  • C/C : clang build and debug active file
  • C/C : clang build and debug active file
  • C/C : g build and debug active file
  • C/C : cpp build and debug active file

But on every try I've encountered following result:

Build finished successfully.
 *  Terminal will be reused by tasks, press any key to close it.

or an error message

The preLaunchTask 'C/C  : g   build active file' terminated with exit code -1.

Is there another way that I can debug c file in vscode on macOS?

My tasks.json file (my c file is in the project root directory)

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C  : clang   build active file",
            "command": "/usr/bin/clang  ",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

My launch.json file

{
    "configurations": [
        {
            "name": "C/C  : clang build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C  : clang build active file"
        }
    ],
    "version": "2.0.0"
}

CodePudding user response:

the launch.json "preLaunchTask": "C/C : clang build active file"
use task.json "label": "C/C : clang build active file" as it's execute task
so you launch.json should be modified
notice change clang to clang to match task label

{
    "configurations": [
        {
            "name": "C/C  : clang build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C  : clang   build active file"
        }
    ],
    "version": "2.0.0"
}
  • Related