In GDB (gnu v 7.1-ubuntu) I am getting really weird results when I try to use strcmp
to determine if two strings are equal. p strcmp("hello","hello")
is giving me the result -145947168
.
Everything I try with strcmp
or strncmp
is returning -145947168
in gdb. What am I doing wrong?
EDIT (thanks to Carl for the pointer to a related answer in the comments): See the answer to this question: How to evaluate functions in GDB?
Apparently sometimes the compiler optimizes out functions called from external libraries, and defining a function in code that calls the function of the external library you want access to in GDB will make it available.
I added this to my code:
#ifdef DEBUG
int mystrcmp(char *a, char *b){
return strcmp(a,b);
}
int mystrncmp(char *a, char *b, int n){
return strncmp(a,b,n);
}
#endif
and then re-made with -DDEBUG -g
to enable the compilation of those helper functions for my gdb debugging.
(gdb) p mystrcmp("hello","hello")
$1 = 0
(gdb) p strcmp("hello","hello")
$2 = -145947168