c - 对 `roundf' 的 undefined reference - C 语言

标签 c compiler-errors compilation ubuntu-14.04 math.h

我是 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 的一部分有一个有趣的话题:

Why do you have to link the math library in C?

关于c - 对 `roundf' 的 undefined reference - C 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465289/

相关文章:

c - 在不使用循环的情况下确定特定元素的数组索引

c - 使用虚拟 shell 时,如何检测用户是否在程序和参数后输入 '&'?

c++ - 初始化结构中的常量数组

c++ - 尝试使用: - catch在C++ C2509中定义教官

compiler-errors - Octave 音阶中未定义的文本扫描

compiler-errors - Ada - 一维数组操作

c++ - 向 Waf 配置添加包含路径 (C++)

关于 C 中同一程序使用不同文件的困惑

c - C 中的 Pthread_cond_wait

c++ - 什么是 C++ 编译性能瓶颈?