【问题标题】:Why the trunc function of C shows me an error? [duplicate]为什么 C 的 trunc 函数显示错误? [复制]
【发布时间】:2021-02-15 08:10:09
【问题描述】:

当我尝试在 C 中运行 trunc 函数时,它给了我一个错误,提示 undefined reference to `trunc' 如何解决这个错误?

错误代码在这里:

/usr/bin/ld: /tmp/ccTANPox.o: in function `floatmod': temp1.c:(.text+0x3a6): undefined reference to `trunc' collect2: error: ld returned 1 exit status

【问题讨论】:

  • 你链接到-lm了吗
  • 注意:在哪里可以找到您需要与-lm 链接的信息? It says so in the man page.
  • @Jabberwocky 我不知道 -lm 先生是什么
  • libm 是 unixy 操作系统上的标准数学库。
  • @Shawn sir 如何将 libm 与我们的程序链接?

标签: c math truncation


【解决方案1】:

如果我用gcc main.c 编译下面的代码,那么我会看到这个错误(和你的类似)

/tmp/ccbouRK0.o: In function `main':
main.c:(.text+0x23): undefined reference to `trunc'
collect2: error: ld returned 1 exit status

编译与gcc -lm main.c 一起工作(您可以在其中看到如何使用-lm)。

#include <math.h>
#include <stdio.h>
int main()
{
   double f=9.8;
   int i;
   i=trunc(f);
   printf("%i",i);
   return 0;    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2021-01-20
    • 1970-01-01
    • 2017-06-30
    • 2018-02-06
    • 2018-01-28
    • 2016-05-21
    • 2023-03-11
    相关资源
    最近更新 更多