tags:

views:

43

answers:

2

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;    
}

$ 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 ask help from Google,

i found the answer,

$ 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. Please guide me in this issue.

+3  A: 

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

Jerry Coffin
A: 

It's not the compiler, but the linker, ld, that is complaining. It cannot find the routine fmod in your program. You have to tell it to link with math library libm with the -l flag.

[Much] more info: GCC, the GNU Compiler Collection.

Nikolai N Fetissov