我是 C 的新手,我一直遇到同样的错误。我在 nano 文本编辑器中输入代码并在 linux 终端中编译它。我的操作系统是 Ubuntu 14.04。这是无法编译的代码示例,
#include <math.h>
#include <stdio.h>
int main()
{
float x = 23.33f;
x = roundf(x);
printf("%f\n", x);
return (0);
}
我收到的错误是,
cc example.c -o example
/tmp/ccWV0Or8.o: In function `main':
example.c:(.text+0x1d): undefined reference to `roundf'
collect2: error: ld returned 1 exit status
make: *** [example] Error 1
非常感谢任何帮助,谢谢!
最佳答案
与数学库的链接:
cc example.c -o example -lm
数学库函数不是默认链接的标准 C 库的一部分。所以你需要自己链接。
关于为什么它不是 libc 的一部分有一个有趣的话题:
关于c - 对 `roundf' 的 undefined reference - C 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465289/