c - 使用uclibc时如何解决fenv.h中对函数的 undefined reference ?

标签 c linux gcc uclibc floating-point-exceptions

我正在尝试测试 fenv.h 中的一些函数,但是,当我编译以下函数时,ld 失败并显示 undefined reference to 'feclearexcept'对“fetestexcept”的 undefined reference 。我正在运行针对 uclibc 编译的强化 gentoo,我怀疑这至少在某种程度上是相关的

#include <stdio.h>      /* printf */
#include <math.h>       /* sqrt */
#include <fenv.h>      
#pragma STDC FENV_ACCESS on

int main ()
{
  feclearexcept (FE_ALL_EXCEPT);
  sqrt(-1);
  if (fetestexcept(FE_INVALID)) printf ("sqrt(-1) raises FE_INVALID\n");
  return 0;
}

fenv.h/usr/include 中。 /usr/lib中有静态库和动态库(libm.alibm.so)。我正在使用 gcc -o test test.c -lm 进行编译;有没有人知道为什么链接器找不到相关函数。 fenv.h 中似乎没有对应的库。

更新:这篇十年前的博文似乎暗示 uclibc 不支持 fenv。我无法确定是否仍然是这种情况,但如果是,还有什么可做的。 http://uclibc.10924.n7.nabble.com/missing-fenv-h-for-qemu-td2703.html

最佳答案

库最后,尝试编译

$ gcc -o test test.c -lm

我用上面的编译语句在我的 x86_64 Linux 系统上尝试了你的确切程序,它构建并运行得很好:

$ gcc -o fenv fenv.c -lm
$ ./fenv
sqrt(-1) raises FE_INVALID

我生成的二进制文件具有以下依赖项:

$ ldd ./fenv
    linux-vdso.so.1 =>  (0x00007ffd924b7000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fca457e8000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca4541e000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fca45af0000)

我还验证了 fenv.h 中的函数确实存在于数学库中:

emil@synapse:~/data/src$ strings /lib/x86_64-linux-gnu/libm.so.6 | grep -E ^fe
feclearexcept
fegetexceptflag
feraiseexcept
fesetexceptflag
fetestexcept
fegetround
fesetround
fegetenv
feholdexcept
fesetenv
feupdateenv
fedisableexcept
feenableexcept
fegetexcept

所以您的设置中可能还有其他问题。

关于c - 使用uclibc时如何解决fenv.h中对函数的 undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191530/

相关文章:

C 程序设计 - 空格在 scanf() 中的作用

无法为 SIGTRAP 注册处理程序

android - 如何修改 AOSP 以允许特定应用程序获得 root 访问权限?

c - 接入点初始扫描

c++ - 函数模板中的 lambda 闭包类型和默认参数

c - 我需要在第 17 行的哪里放置额外的分号

c - 如何获得程序的完整输入流?

c - X11 在移动窗口和调整窗口大小时进行干预

c++ - 部分模板参数应用程序的部分模板特化不适用于 GCC 4.8.1

ruby - bash 管道在 ruby​​ #system 中不起作用,并且在 ruby​​ Shell#system 中不起作用