I had a code that was working perfectly in windows but when i tried to compile it in linux i got an error. I found that the issue is with the sin() function.
If i pass to it a constant number directly, it works fine:
#include <stdio.h>
#include <math.h>
int main(void) {
float y = sin(1.57);
printf("sin(1.57) = %f", y);
return 0;
}
sin(1.57) = 1.000000
But when i pass to it a variable i get an error:
#include <stdio.h>
#include <math.h>
int main(void) {
float x = 1.5;
float y = sin(x);
printf("sin(%f) = %f", x, y);
return 0;
}
/tmp/ccfFXUZS.o: In function `main':
source-bcfaa9ff162b:(.text+0x1a): undefined reference to `sin'
collect2: error: ld returned 1 exit status
On some systems, the math functions are in a separate library, which needs to be linked explicitly. Simply pass the option -lm when compiling (linking) your program, and everything should work.
The reason the first one works is because the compiler knows about sin and optimized the whole thing to a constant - it doesn't need to actually call the sin function.
You need to link with -lm.
I don't know why the first example is working - maybe some optimizations ?