4

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • That sounds odd. Shouldn't need to do anything special to run in gdb. Can you please provide complete logs of the compilation and gdb run? – kaylum Nov 19 '20 at 06:30
  • What makes you think that you should use GDB with some key like `-lm`? – ks1322 Nov 19 '20 at 11:20

2 Answers2

2

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)

soni
  • 77
  • 1
  • 9
0

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
Berdan Akyürek
  • 212
  • 1
  • 14