Configuration for debugging C program in visual studio code in MacOS - c

I am trying to configure my vsc for debugging C program by following the following article. However, I am getting this error while compiling the program. Can please kindly advise what is the problems here.
Error: there is no registered task type 'cppbuild'. Did you miss to install an extension that provides a corresponding task provider?
other error
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": "clang - Build and debug active file",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [
],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
}
gcc version
steh#u81z08s03 ft_printf % gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.28)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Related

Use gdb in vscode with -lm flag

As of https://code.visualstudio.com/docs/cpp/launch-json-reference, I can only attach to an already existing binary with vscode (in the example being a.out), but I would like to have it in a way so that I can debug without having to compile manually first (with the -lm flag). How would I go about that?
Figured it out. Instead of only using a launch.json, I also used a task.json as follows in order to build the c file first:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C file",
"type": "shell",
"command": "gcc -g -o ${workspaceFolder}/sum/main ${file} -lm",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the launch.json, I use the task as a preLaunchTask:
{
"version": "0.2.0",
"configurations": [
{
"name": "C Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/sum/main", // Dummy value
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build C file",
}
],
}
More info on tasks here: https://code.visualstudio.com/docs/editor/tasks

Passing arguments to the C debugger in Visual Studio Code

As the title says, I'm not able to pass arguments to the Visual Studio Code debugger.
I'm in MacOS, and I'm using gcc.
When I use the auto-generated launch.json and tasks.json everything's fine, but when I add the arguments everything crashes.
auto-generated 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": "gcc - Compilar y depurar el archivo activo",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: gcc build active file"
}
]
}
auto-generated tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
When I first execute it, I can debug freely, but as I'm adding (to launch.json):
"args": [
"-c",
"1",
"file1"
],
everything's crashes with the message:
The preLaunchTask 'C/C++: gcc build active file' terminated with exit code -1.
and if I click Debug Anyway the message becomes:
launch: program '/Users/.../.vscode/launch' does not exist*
At that point I can't do anything else but click on Open launch.json
Could you help me?
Thanks

VScode's debugger not working [MacOS - Clang - C]

My problem is generated when trying to debug C (the programming language) in vscode using the latest MacOS version with command line tools installed.
The error appears when the external terminal is launched in order to debug. I'm using the clang compiler with the following configuration files for the debugger in vscode:
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": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"label": "Open Terminal",
"type": "shell",
"command": "osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'",
"problemMatcher": []
},
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The error that appears in the terminal:
username#Martins-MBP ~ % cd ''; env 'USER=Martin' '__CFBundleIdentifier=com.microsoft.VSCode' 'COMMAND_MODE=unix2003' 'LOGNAME=Martin' 'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' 'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ABVM6fu0pS/Listeners' 'SHELL=/bin/zsh' 'HOME=/Users/Martin' '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x2' 'TMPDIR=/var/folders/zb/b62pl9hd43n4chv3k7jg5cxr0000gn/T/' 'XPC_SERVICE_NAME=application.com.microsoft.VSCode.142509.142515' 'XPC_FLAGS=0x0' 'ORIGINAL_XDG_CURRENT_DESKTOP=undefined' 'SHLVL=0' 'PWD=/' 'OLDPWD=/' '_=/Applications/Visual Studio Code.app/Contents/MacOS/Electron' 'APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=true' '/bin/sh' '/var/folders/zb/b62pl9hd43n4chv3k7jg5cxr0000gn/T/Microsoft-MIEngine-Cmd-ukaihz0n.2gz'

Linking math library to visual studio code GCC

I'm trying to link the math library to the C debugger than comes with visual studio code, GCC. I'm want to mimic what the argument -lm does when I use a makefile. To my understanding I can do this using the launch.json file shown below.
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["nums"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I looked through the documentation https://code.visualstudio.com/docs/cpp/launch-json-reference. The closest thing I could find was "miDebuggerArgs". I tried adding the line "miDebuggerArgs": "-lm" to the "configurations" list, but it didn't work. Any advice?
You posted the launch.json file. Go to the tasks.json and add -lm to the arguments.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build"
},
"detail": "compiler: /usr/bin/gcc"
}
]
}

How can I run my debugger commands while the program is running?

I've configured my launch.json & tasks.json then return to my file.c and click the green arrow.
I see 'attached image'. Click OK to allow access then I am left with my Debugger but no variables exist and my options like step_into, step_over disappear then I find myself in the same loop.
Below is the JSON for launch and tasks.
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": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang build active file"
}
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
"/property:GenerateFullPaths=true",
"/t:build",
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
It looks like the code is not "paused", so the debugger will not activate while the code is actively running. Try to set a breakpoint and see if that fixes the issue.

Resources