While debugging a C program in gdb I have a breakpoint in a for loop. I cannot print the value of \"i\" ( I get : No symbol \"i\" in current context.). I can print the value of
In case anyone else is using Google's Bazel build system for your project, I'd like to add that if you cannot print any variables from gdb
, it may be because you need to properly add the -ggdb
and -O0
(update: use -Og instead of -O0 use -O0
over -Og
) C build flags using the --copt=
option, INSTEAD OF using the --per_file_copt=
option. In my case, although they both built and ran just fine, only the --copt=
technique allowed me to fully use gdb and print variables, whereas the --per_file_copt=
one also allowed me to use gdb but would NOT allow me to print variables.
Note: in the below examples, just replace test
with build
if you do NOT need to run the unit tests as well.
UDPATE: it turns out, you should prefer See here: What's the difference between a compiler's `-O0` option and `-Og` option?.-Og
over -O0
when doing debugging, so I'm updating these examples accordingly.
So, do this:
time bazel test --copt=-ggdb --copt=-O0 \
//my/build/folder1/... //my/build/folder2/...
INSTEAD OF this:
time bazel test --per_file_copt=//my/build/folder1/...,//my/build/folder2/...@-ggdb,-O0 \
//my/build/folder1/... //my/build/folder2/...
...in order to be able to print variables from within gdb.
Again, both of the above techniques build and run just fine, and both allow me to run and use gdb, but only the first one actually allows me to use gdb
to its full extent.
Lastly, if the first command above still doesn't work, try adding the --strip=never
Bazel flag described here to prevent Bazel from ever stripping debugging information. Now the command will look like this:
time bazel test --copt=-ggdb --copt=-O0 --strip=never \
//my/build/folder1/... //my/build/folder2/...
--copt=
:
--per_file_copt
:
--strip=never
:
-Og
over -O0
-O0
over -Og
for debugging: What's the difference between a compiler's `-O0` option and `-Og` option?