未定义的对 `sin` 的引用 [英] Undefined reference to `sin`

查看:65
本文介绍了未定义的对 `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;
}

使用 gcc 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

然而,我编写了各种测试程序,从 main 函数中调用 sin,并且这些程序运行良好.我一定在这里做错了什么 - 但那是什么?

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?

推荐答案

您已经使用对正确 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.

正如 Paul 已经提到的,在您尝试生成可执行文件的步骤中添加-lm"以与数学库链接.

As Paul has already mentioned add "-lm" to link with the math library in the step where you are attempting to generate your executable.

评论中,linuxD 问:

为什么<math.h>中的sin(),我们是否需要明确的-lm选项;但,不适用于 中的 printf()?

Why for sin() in <math.h>, do we need -lm option explicitly; but, not for printf() in <stdio.h>?

因为这两个函数都是作为单一 UNIX 规范"的一部分实现的.该标准的这段历史很有趣,并以许多名称为人所知(IEEE Std 1003.1、X/Open Portability Guide、POSIX、Spec 1170).

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).

这个标准,专门将标准C库"例程与标准C库"程序区分开来数学库"例程(第 277 页).相关段落抄录如下:

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 库被自动搜索cc 解析外部引用.该库支持所有基本系统的接口,如第 1 卷中所定义,除了数学例程.

The Standard C library is automatically searched by cc to resolve external references. This library supports all of the interfaces of the Base System, as defined in Volume 1, except for the Math Routines.

标准C数学库

这个库支持基本系统数学例程,如第 1 卷中所定义. cc 选项-lm 用于搜索这个库.

This library supports the Base System math routines, as defined in Volume 1. The cc option -lm is used to search this library.

这种分离背后的原因受到多种因素的影响:

The reasoning behind this separation was influenced by a number of factors:

  1. UNIX 战争导致与最初的 AT&T UNIX 产品的分歧越来越大.莉>
  2. UNIX 平台的数量增加了为操作系统开发软件的难度.
  3. 尝试为软件开发人员定义最低公分母,称为 1988 POSIX.
  4. 软件开发人员根据 POSIX 标准进行编程,以便在符合 POSIX 的系统"上提供他们的软件,以便覆盖更多平台.
  5. UNIX 客户需要符合 POSIX 标准"的 UNIX 系统来运行该软件.
  1. The UNIX wars led to increasing divergence from the original AT&T UNIX offering.
  2. The number of UNIX platforms added difficulty in developing software for the operating system.
  3. An attempt to define the lowest common denominator for software developers was launched, called 1988 POSIX.
  4. Software developers programmed against the POSIX standard to provide their software on "POSIX compliant systems" in order to reach more platforms.
  5. UNIX customers demanded "POSIX compliant" UNIX systems to run the software.

决定将 -lm 放在不同的库中的压力可能包括但不限于:

The pressures that fed into the decision to put -lm in a different library probably included, but are not limited to:

  1. 这似乎是减小 libc 大小的好方法,因为许多应用程序不使用嵌入在数学库中的函数.
  2. 它在数学库实现方面提供了灵活性,其中一些数学库依赖于较大的嵌入式查找表,而其他数学库可能依赖于较小的查找表(计算解决方案).
  3. 对于真正受大小限制的应用程序,它允许以非标准方式重新实现数学库(例如仅提取 sin() 并将其放入自定义构建的库中.
  1. 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.
  2. 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).
  3. 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.

无论如何,它现在是标准的一部分,不会被自动包含为 C 语言的一部分,这就是您必须添加 -lm 的原因.

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.

这篇关于未定义的对 `sin` 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆