This question already has answers here:
Closed last month.
I noticed that when I use sin
inside function the compiler don't recognize it, here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float sinus(float a){
return sin(a);}
int main(int argc, char **argv)
{
double a = sinus(2);
printf("%f \n", sin(2));
printf("%f", a);
return 0;
}
If I use it directly in main it works fine, but inside a user defined function it gives me this error undefined reference to sin
.
For compiling I use gcc -Wall -lm -lc -lgcc -o "%e" "%f"
.
References to libraries typically go to the end of the command line, in particular after the sources have been specified:
gcc -Wall -o "%e" "%f" -lm
(specifing the C lib is not necessary, it is linked implicilty)
From the documentation:
-l library
[...]
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.