对“sin”的未定义引用
我有以下代码(针对这个问题精简到最基本的内容):
#include<stdio.h>
#include<math.h>
double f1(double x)
{
double res = sin(x);
return 0;
}
/* The main function */
int main(void)
{
return 0;
}
当使用 gcc test.c
编译它时,我收到以下错误,我无法弄清楚为什么:
/tmp/ccOF5bis.o: In function `f1':
test2.c:(.text+0x13): undefined reference to `sin'
collect2: ld returned 1 exit status
但是,我我已经编写了各种从 main
函数中调用 sin
的测试程序,并且这些程序运行得很好。我一定在这里做了一些明显错误的事情 - 但它是什么?
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;
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,在我最后列出我的库后,这个问题就消失了: gcc prog.c -lm
I had the same problem, which went away after I listed my library last: gcc prog.c -lm
您已经使用正确的 math.h 头文件的引用编译了代码,但是当您尝试链接它时,您忘记了包含数学库的选项。因此,您可以编译 .o 目标文件,但不能构建可执行文件。
正如 Paul 已经提到的,在尝试生成可执行文件的步骤中添加“
-lm
”以链接到数学库。在评论中,linuxD 询问:
因为这两个功能都是作为“单一 UNIX 规范”的一部分实现的。该标准的历史很有趣,并且有许多名称(IEEE Std 1003.1、X/Open 可移植性指南、POSIX、Spec 1170)。
该标准专门将“标准 C 库”例程与“标准 C 数学库”分开例程(第 277 页)。相关段落抄录如下:
这种分离背后的原因受到许多因素的影响:
决定将
-lm
放入不同的库中的压力可能包括但不限于:sin()
并将其放入自定义构建的库中。无论如何,现在是标准的一部分不会自动包含为 C 语言的一部分,这就是您必须添加
-lm
的原因。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.
As Paul has already mentioned add "
-lm
" to link with the math library in the step where you are attempting to generate your executable.In the comment, linuxD asks:
Because both these functions are implemented as part of the "Single UNIX Specification". This history of this standard is interesting, and is known by many names (IEEE Std 1003.1, X/Open Portability Guide, POSIX, Spec 1170).
This standard, specifically separates out the "Standard C library" routines from the "Standard C Mathematical Library" routines (page 277). The pertinent passage is copied below:
The reasoning behind this separation was influenced by a number of factors:
The pressures that fed into the decision to put
-lm
in a different library probably included, but are not limited to:sin()
and putting it in a custom built library.In any case, it is now part of the standard to not be automatically included as part of the C language, and that's why you must add
-lm
.我仍然遇到添加
-lm
的问题:我最近发现,如果您先指定
-lm
,它就无法工作。顺序很重要。您必须指定-lm
last,如下所示:链接没有问题。
因此,您必须在最后指定库。
I still have the problem with
-lm
added:I discovered recently that it does not work if you specify
-lm
first. The order matters. You must specify-lm
last, like this:That links without problems.
So, you must specify the libraries at the end.
您需要链接数学库 libm:
You need to link with the math library, libm: