How to fix ignored breakpoints when debugging C in VScode (gdb)? - debugging

I am trying to debug my C in vscode using breakpoints, but the debugger seems to skip them everytime i run it (the break points change colours from red to grey). I looked at this question which is essentially the same question i have. I tried all the answers there and none worked (none were set as 'answers' by the person who asked, hence why i am asking this question again). So my question is, how to get vscode breakpoints working in C?
Vscode version: 1.73.1 on windows 10
gdb version: 12.1
launch.json
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\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
}
],
"preLaunchTask": "C/C++: gcc.exe build active file",
}
]
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "make",
"args": [
"all"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
makefile
dynamic_array: dynamic_array.c dynamic_array.h
gcc -c dynamic_array.c
test: test.c dynamic_array.h
gcc -c test.c
all: dynamic_array.o test.o
gcc -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out

Your makefile has a number of problems. First, the target you build is all, which depends on dynamic_array.o and test.o, however, there is no rule for building these, only a rule for building dynamic_array and test.
As such, make will use its default rule for building a .o file, which is:
$(CC) $(CPPFLAGS) $(CFLAGS) -c
This leads to the next problem, which is probably the cause of the breakpoint problems you are seeing - neither the implicit rule, nor the rule you tried to write, include the -g flag for gcc. The -g flag turns on production of debug information.
I would at a minimum, rewrite the makefile as:
dynamic_array.o: dynamic_array.c dynamic_array.h
gcc -c -g3 dynamic_array.c
test.o: test.c dynamic_array.h
gcc -c -g3 test.c
all: dynamic_array.o test.o
gcc -g3 -o test.exe dynamic_array.o test.o
clean:
del -f *.o & del -f *.exe & del -f *.out

Related

How to setup sublime text 3 to run on C++11, but it opens the cmd each run?

How do I run C++11 in Sublime Text 3?
I found this and this works, but I want it to be able to open cmd each run.
(1)
{
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
Like this one:
(2)
{
"cmd":
[
"g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o",
"${file_base_name}.exe", "&&", "start",
"cmd", "/k" , "$file_base_name"
],
"selector": "source.cpp",
"working_dir": "${file_path}",
"shell": true
}
The problem with (1) is it runs with the console inside Sublime Text 3, I don't want that, unfortunately.
The problem with (2) is it runs well and it opens CMD each time, which is what I need, but it's on C++98. When I need C++11.
So, is there a way to modify any one of these build systems so that I can run C++11 and make it open CMD every run (instead of running it on the console)? Thanks.
I was able to figure it out with a little bit of tinkering involved.
{
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}\" && start cmd /k \"${file_path}/${file_base_name}\""
}
]
}

gcc.exe Invalid Argument Fatal Error no input files (VScode)

I'm trying to debug my code and am having problems with running the build task. These are the error messages:
Starting build...
C:\MinGW\bin\gcc.exe -fdiagnostics-color=always -g "C:\Users\Sandra study\Documents\2022b\מעבדה C\project maman 14\firstIteration.c" -o "C:\Users\Sandra study\Documents\2022b\מעבדה C\project maman 14\firstIteration.exe"
gcc.exe: error: C:\Users\Sandra study\Documents\2022b\????? C\project maman 14\firstIteration.c: Invalid argument
gcc.exe: fatal error: no input files
compilation terminated.
Build finished with error(s).
My tasks.json:
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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname} \\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${Project Maman 14}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
If I try debugging then I get this error message:
I'm new to VScode and really don't understand how to use these files any help would be much appreciated

gcc: error: unrecognized command line option ‘--interpreter=mi’ in Visual Studio Code

I just installed Visual Studio Code in Ubuntu 18.04 LTS based ElementaryOS 5.1 Hera, and set up the environment for C/C++. Launch.json and tasks.json look all fine but on debugging, the following errors are triggered:
gcc: error: unrecognized command line option ‘--interpreter=mi’
gcc: error: unrecognized command line option ‘--tty=/dev/pts/0’
gcc: fatal error: no input files compilation terminated.
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 - 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/gcc"
}
]
}
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"
}
Is there any way to fix this?

Linking mac framework in VScode

I'm struggling to build an openGL project in VScode on Mac. The problem is how to add a framework in the tasks.json file of VScode. I can build the project successfully from the command line.
To build successfully I need the gcc command to include the argument -framework OpenGL. So it should read:
gcc *.cc -std=c++17 -stdlib=libc++ -g -framework OpenGL ...(etc.)
but if I add "-framework OpenGL" to the argument list in tasks.json, VScode parses it as:
gcc *.cc -std=c++17 -stdlib=libc++ -g '-framework openGL' ...(etc.)
Obviously gcc doesn't recognise '-framework OpenGL' in the inverted commas.
How do I add a framework in VSCode on a mac???
My tasks.json is:
"type": "shell",
"label": "C/C++: clang build active file",
// "command": "/usr/bin/clang",
"command": "gcc",
"args": [
"${fileDirname}/*.cc",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-framework openGL",
"-L/usr/local/Cellar/glew/2.1.0_1/lib",
"-L/usr/local/Cellar/glfw/3.3.2/lib/",
"-lglfw",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
Try putting them in separate rows like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-v",
"-I/usr/local/include/GLFW",
"-L/usr/local/lib",
"-lglfw3",
"-framework",
"Cocoa",
"-framework",
"OpenGL",
"-framework",
"IOKit"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
So:
"-framework",
"Cocoa",
Even my answer comes 4 years later I hope will help you.
The solution is to write the c_cpp_properties.json file (found or created by yourself in hidden .vscode folder inside your project) such as:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"intelliSenseMode": "macos-gcc-x64",
"configurationProvider": "go2sh.cmake-integration"
}
],
"version": 4
}
As you can see, you have to add there this magic sentence:
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
Of course this is the path where my system keeps all the frameworks. You have to find and to fill your own system path.

VS code gdb exiting immediately when run as vs code debugger

When I debug my application through a terminal and gdb, everything works as expected, however if I try to use gdb as the debugger for vs code, it exists immediately with output:
Breakpoint 1, main (argc=1, argv=0x7fffffffe638) at /home/kronos/Desktop/voxel-world/source/main.cpp:60
60 {
[Inferior 1 (process 7771) exited with code 01]
The program '/home/kronos/Desktop/voxel-world/build/voxel-world' has exited with code 1 (0x00000001).
This is my configuration file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/voxel-world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
First I would make sure that the program is not exiting with return code 1 by design (e.g., due to sanity checks etc.).
You could start by setting
"stopOnEntry": true
in config file launch.json, then proceed by debugging stepwise through main().
The solution that worked for me was to recompile everything with no optimization -O0 and with debugging symbols included -g. This is what my Makefile looks like:
all: main
main: main.o flash_simulator.o flash_simulator.h
cc -O0 -g -o main main.o flash_simulator.o
%.o : %.c
cc -O0 -g -c $< -o $#
clean:
rm -f *.o

Resources