How to use GCC for compilation and debug on VSCode - c

I have found out a few different ways to debug C with the GCC compiler, however I want to have debugging on by default. Is there any way I can do this with a setting in my launch.json in VSCode?
Here is my 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

Add these to your CFLAGS: -gdwarf-4 -g3
To do that, run this command:
export CFLAGS="${CFLAGS} -gdwarf-4 -g3"
See this link for more information and this link for running commands in launch.json.

Related

VScode configuration causes code to not execute

I am returning to learning C and was trying to get a basic code to print something to run, and for some reason, after debugging, and writing in the terminal ./{filename}.exe, I get no output at all, and a message in the terminal that says bash: ./test.exe: No such file or directory. attached below is the configuration of the launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/c_projects.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/cygwin64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
Thanks in advance!

Unable to start debugging. Unexpected GDB output from command"-environment-cd path to my c folder". -environment-cd: Usage DIRECTORY

So i wanted to learn c and got this error after trying to debug it:
Unable to start debugging. Unexpected GDB output from command"-environment-cd path to my c folder". -environment-cd: Usage DIRECTORY
My 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "C:\\Users\\Šimon\\Desktop\\Cfiles\\HelloWorld.c",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Users\\Šimon\\Desktop\\migw64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
If you want more stuff then ask me.
I'm using visual studio code.

&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"

I'm currently using vscode on ubuntu to write and debug a simple c program.
I've updated gdb to the newest version(9.2-0ubuntu1~20.04.1). Whenever I click the debug button a "Permission Denied" error is shown.
Here's a screenshot of the error message
How do I resolve this error so that I can debug my C program with GDB and VSCode? I would appreciate it. The problem has bothered me for days and I can't figure it out myself.
This code snippet below is my launch.json file.
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

Visual Studio Code Property Console not allowed error

i am new to programming and i was doing some of these questions of a book.And got stuck in this question about area of a triangle."Property console is not allowed".Is it due to the math.h?
Because the same code is fully functional on online-gdb.
here is content of lauch.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 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"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"
}
]
}
Replace
"console": "externalTerminal"
with
"externalConsole": true
Seems like "console" property is not supported in this version of auto-generated launch.json. However this does the trick to get rid of the warning message for me.
Hmm, now I'm getting "Property externalConsole is not allowed"

How to debug in vscode already compiled executable?

When trying to debug in vscode, the vscode generate launch.json config file. When I try to debug, it first tries to compile the source and then debug the exe. I would like to just debug already compiled executable (compiled with gcc -g option).
The json file is:
{
// 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-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [
"-pthread",
"common.c"
],
"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-10 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
Can I make somehow the vsode to not compile but just debug with already compiled source to "program":"${fileDirname}/${fileBasenameNoExtension}", I have compiled to the very same name - no extension in current directory, as the property suggest. So how to make vscode to take this executable as its debug file?

Resources