-
error when using math.h
Hi all,
I have the following code:
Code:
#include <math.h>
...
...
double x;
int y, i;
x = floor( (double)y/4 );
i = (int)fmod(x, 2);
and i always got the following errors:
undefined reference to 'floor'
undefined reference to 'fmod'
What s wrong with the code? I already include math.h...
Thank you!
-
With that code I see problem you dind't initialize y. Aside that I see no other problems. Which compiler are you using?
-
I know already...
I need to add -lm into the command gcc
-
But still don't know what is the reason of adding -lm...
-
The math library is where your math functions are defined. The header just tells the compiler that some functions exist and how they are to be used. The linker needs to know where these functions are.
-
The linker must know which libraries to link to. With -lm, you link to functions from math.h.
-
The reason people forget to add the -lm when using the math library is because they think it is part of the standard. Well it is, but they decided to link the c library automatically to all programs, just not the math library.