undefined reference to `sin', even though I use <math.h> and -lm [duplicate]

僤鯓⒐⒋嵵緔 提交于 2020-01-20 08:56:51

问题


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:


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.



来源:https://stackoverflow.com/questions/59503933/undefined-reference-to-sin-even-though-i-use-math-h-and-lm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!