即使我使用< math.h> ;,也未定义对"sin"的引用和-lm [英] undefined reference to `sin', even though I use <math.h> and -lm
问题描述
我注意到,当我在函数内部使用sin
时,编译器无法识别它,这是一个示例:
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;
}
如果我直接在main中使用它,则可以正常工作,但是在用户定义的函数中,它将出现此错误undefined reference to sin
.
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
.
对于编译,我使用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
(指定C库不是必需的,这是隐含的链接)
从文档:
-l <em>库
[...]
在命令中写入此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件.因此,"foo.o -lz bar.o"在文件foo.o之后但在bar.o之前搜索库"z".如果bar.o引用了"z"中的函数,则可能不会加载这些函数.
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.
这篇关于即使我使用< math.h> ;,也未定义对"sin"的引用和-lm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!