undefined reference to `sin', even though I us

2020-02-15 06:20发布

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".

1条回答
家丑人穷心不美
2楼-- · 2020-02-15 06:58

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.

查看更多
登录 后发表回答