How to use math functions with gdb

Viewed 453

I am a student and I faced a problem: When I use pow or asin in my Linux programs and try to debug it with GDB I get the error: undefined reference to 'pow'.

I know that to fix this in the GCC compiler, I need to add the -lm key. Is there some key like -lm for GDB?

2 Answers

To use the math function, you have to compile the source code with -lm option (this will remove the undefined reference error for math functions), and for making debugging symbols available in gdb, you have to compile source code with -g option.

gcc -g -o myprog main.c -lm

To debug the program run this with

gdb ./myprog

To print or use any function during debugging with gdb, use call function of gdb

call (double)pow(3.0, 2.0)

Make sure to use the correct syntax of function, otherwise gdb return wrong answers

call (double) pow (double , double)

gdb uses compiled file to debug. So after using -lm and -g, you can run the resulting file normally using gdb. In other words, you should be running the following which works for me:

gcc -g test.c -lm
gdb ./a.out
Related