未定义参考`sin` [英] Undefined reference to `sin`
问题描述
我有以下的code(精简裸基础知识这个问题):
I have the following code (stripped down to the bare basics for this question):
#include<stdio.h>
#include<math.h>
double f1(double x)
{
double res = sin(x);
return 0;
}
/* The main function */
int main(void)
{
return 0;
}
在与编译它海合会test.c以
我碰到下面的错误,我不能工作了原因:
When compiling it with gcc test.c
I get the following error, and I can't work out why:
/tmp/ccOF5bis.o: In function `f1':
test2.c:(.text+0x13): undefined reference to `sin'
collect2: ld returned 1 exit status
不过,我已经写了调用各种测试程序罪
从主
函数中,而那些工作完美。我必须明明在这里做得不对? - 但它是什么
However, I've written various test programs that call sin
from within the main
function, and those work perfectly. I must be doing something obviously wrong here - but what is it?
推荐答案
您已经编译了code与正确math.h中头文件的引用,但是当你试图连接它,你忘了选择包括数学库。这样一来,就可以编译.o目标文件,但不建立自己的可执行文件。
You have compiled your code with references to the correct math.h header file, but when you attempted to link it, you forgot the option to include the math library. As a result, you can compile your .o object files, but not build your executable.
由于保罗已经提到添加-lm,包括在您正试图生成可执行步骤的数学库。
As Paul has already mentioned add "-lm" to include the math library in the step where you are attempting to generate your executable.
这篇关于未定义参考`sin`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!