C语言 未定义的对 `sin` 的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5005363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to `sin`
提问by robintw
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.cI get the following error, and I can't work out why:
使用编译时gcc test.c出现以下错误,我不知道为什么:
/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 sinfrom within the mainfunction, and those work perfectly. I must be doing something obviously wrong here - but what is it?
但是,我已经编写了sin从main函数内部调用的各种测试程序,并且这些程序运行良好。我一定在这里做错了什么——但那是什么?
回答by Edwin Buck
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.
您已经使用对正确 math.h 头文件的引用编译了代码,但是当您尝试链接它时,您忘记了包含数学库的选项。因此,您可以编译 .o 对象文件,但不能构建可执行文件。
As Paul has already mentioned add "-lm" to link with the math library in the step where you are attempting to generate your executable.
正如保罗已经提到-lm的,在您尝试生成可执行文件的步骤中,添加“ ”以与数学库链接。
Why for
sin()in<math.h>, do we need-lmoption explicitly; but, not forprintf()in<stdio.h>?
为什么
sin()in<math.h>,我们是否需要-lm明确的 option ;但是,不是为了printf()in<stdio.h>?
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).
因为这两个函数都是作为“单一 UNIX 规范”的一部分实现的。这个标准的历史很有趣,并以许多名字为人所知(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:
该标准专门将“标准 C 库”例程与“标准 C 数学库”例程(第 277 页)区分开来。相关段落抄录如下:
Standard C Library
The Standard C library is automatically searched by
ccto resolve external references. This library supports all of the interfaces of the Base System, as defined in Volume 1, except for the Math Routines.Standard C Mathematical Library
This library supports the Base System math routines, as defined in Volume 1. The
ccoption-lmis used to search this library.
标准 C 库
自动搜索标准 C 库
cc以解析外部引用。该库支持基本系统的所有接口,如第 1 卷中所定义,数学例程除外。标准 C 数学库
该库支持基本系统数学例程,如第 1 卷中所定义。该
cc选项-lm用于搜索该库。
The reasoning behind this separation was influenced by a number of factors:
这种分离背后的原因受到许多因素的影响:
- The UNIX warsled to increasing divergence from the original AT&T UNIX offering.
- The number of UNIX platforms added difficulty in developing software for the operating system.
- An attempt to define the lowest common denominator for software developers was launched, called 1988 POSIX.
- Software developers programmed against the POSIX standard to provide their software on "POSIX compliant systems" in order to reach more platforms.
- UNIX customers demanded "POSIX compliant" UNIX systems to run the software.
- 在UNIXWar导致从原来的AT&T UNIX发行增加发散。
- UNIX 平台的数量增加了为操作系统开发软件的难度。
- 发起了为软件开发人员定义最低公分母的尝试,称为 1988 POSIX。
- 软件开发人员根据 POSIX 标准进行编程,以在“符合 POSIX 的系统”上提供他们的软件,以便覆盖更多平台。
- UNIX 客户需要“符合 POSIX 标准”的 UNIX 系统来运行该软件。
The pressures that fed into the decision to put -lmin a different library probably included, but are not limited to:
决定放入-lm不同图书馆的压力可能包括但不限于:
- It seems like a good way to keep the size of libc down, as many applications don't use functions embedded in the math library.
- It provides flexibility in math library implementation, where some math libraries rely on larger embedded lookup tables while others may rely on smaller lookup tables (computing solutions).
- For truly size constrained applications, it permits reimplementations of the math library in a non-standard way (like pulling out just
sin()and putting it in a custom built library.
- 这似乎是减小 libc 大小的好方法,因为许多应用程序不使用嵌入在数学库中的函数。
- 它在数学库实现中提供了灵活性,其中一些数学库依赖于较大的嵌入式查找表,而其他数学库可能依赖于较小的查找表(计算解决方案)。
- 对于真正大小受限的应用程序,它允许以非标准方式重新实现数学库(例如将其取出
sin()并放入自定义构建的库中。
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.
无论如何,它现在是标准的一部分,不会被自动包含为 C 语言的一部分,这就是您必须添加-lm.
回答by Anyeos
I have the problem anyway with -lm added
无论如何,我有添加 -lm 的问题
gcc -Wall -lm mtest.c -o mtest.o
mtest.c: In function 'f1':
mtest.c:6:12: warning: unused variable 'res' [-Wunused-variable]
/tmp/cc925Nmf.o: In function `f1':
mtest.c:(.text+0x19): undefined reference to `sin'
collect2: ld returned 1 exit status
I discovered recently that it does not work if you first specify -lm. The order matters:
我最近发现,如果您首先指定 -lm,它就不起作用。顺序很重要:
gcc mtest.c -o mtest.o -lm
Just link without problems
只是链接没有问题
So you must specify the libraries after.
所以你必须在之后指定库。
回答by Paul R
You need to link with the math library, libm:
您需要链接数学库 libm:
$ gcc -Wall foo.c -o foo -lm
回答by blackappy
I had the same problem, which went away after I listed my library last: gcc prog.c -lm
我有同样的问题,在我最后列出我的库后就消失了:gcc prog.c -lm

