0

I use Ubuntu 20.04. I write my C program using default text editor and compile the same using the following Syntax -

gcc code.c -o codeObjectName

I'm facing the problem almost/exactly similar to this: Undefined reference to pow( ) in C, despite including math.h

I have a C program where I have used the standard pow function from math.h header file. I have even Included math.h header file aswell in code. But when I compile, I'm getting this error message on my terminal -

/usr/bin/ld: /tmp/xxxxxxxx.o: in function `numerize':
code.c:(.text+0x73): undefined reference to `pow'
collect2: error: ld returned 1 exit status

In the answer to the above attached question, it was given that the linker couldn't find the definition for 'pow' function, for this to work, we have to manually link it with math library.

My question is, even though I included math.h header file, why doesn't the compiler/linker link the libm.a library automatically, just like the libraries of all the functions like prinf, scanf, malloc, etc. are automatically linked?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • Historically, some machines didn't have hardware floating point, (80386 with optional 80387 maths coprocessor, for example). Then what maths library to link depended on the hardware for the machine (using software emulation for maths if the was no coprocessor, etc). Nowadays, this separation is less beneficial. If it is any help, on Macs you don't need to specify `-lm` (though no harm is done if you do); you don't need to specify `-lpthread` either. The standard library includes those functions. This allows you to be lazy on macOS, but requires build changes when you migrate to Linux. – Jonathan Leffler Apr 08 '22 at 18:04

0 Answers0