gcc gives error while using fmod()

mohit picture mohit · Jul 9, 2010 · Viewed 11.3k times · Source

Sample code for fmod:

#include <stdio.h>    
#include <math.h>

int main(void)   
{    
  double x = 0.14527, y = 3.14159;   
  printf("fmod(x, y) = %.6lf\n", fmod(x, y));    
  return 0;    
}

Compiling:

$ gcc main.c -o main

I get

/tmp/ccztJO01.o: In function `main':

main.c:(.text+0x4d): undefined reference to `fmod'

collect2: ld returned 1 exit status

Then I found this in Google:

$ gcc -lm main.c -o main

Why should I use -lm, what is it exactly? From where I can get more information about gcc in detail?

Answer

Jerry Coffin picture Jerry Coffin · Jul 9, 2010

-lm is simply telling it to link libm, which contains all the floating point math routines, including (no surprise here) fmod.