【问题标题】:How to solve undefined reference to functions in fenv.h when using uclibc?使用 uclibc 时如何解决对 fenv.h 中函数的未定义引用?
【发布时间】:2015-10-17 21:01:36
【问题描述】:

我正在尝试测试fenv.h 中的一些函数,但是,当我编译下面的函数时,ld 以undefined reference to 'feclearexcept'undefined reference to 'fetestexcept' 失败。我正在运行针对 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

【问题讨论】:

  • 链接器能找到sqrt()吗?
  • 我运行了准确的发布代码。 ubuntu linux 14.04 和 gcc。编译器提出以下问题:警告:忽略#pragma STDC FENV_ACCESS。但它没有任何问题找到 feclearexcept() 函数或值 FE_ALL_EXCEPT 和 FE_INVALID。您的 /usr/include 目录中的 fenv.h 文件是否应如此?
  • 是的。我运行强化的 uclibc gentoo linux,所以我怀疑它的特殊性是问题所在。它绝对有效的代码,并且可以在大多数系统上编译。

标签: c linux gcc uclibc floating-point-exceptions


【解决方案1】:

库放在最后,尝试编译

$ 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

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

【讨论】:

  • 您能取消将此标记为重复吗? sqrt 使用 -lm 的两个位置正确定义。问题似乎仅限于 fenv.h 中的函数,我真的可以使用答案。
  • @ragingSloth sqrt 应用于常量并不会导致问题并不能证明数学库已正确链接。有一个 sqrt 汇编指令,您将其应用于无论如何都是一个常数。
  • 是的,我使用的是 uclibc 而不是 gnu libc,所以我认为这是问题的一部分。有趣的是,libm 的静态和动态版本都没有列出任何相关函数。
【解决方案2】:

最近的 uclib-ng 记录了缺乏对 fenv.h 的支持:https://cgit.uclibc-ng.org/cgi/cgit/uclibc-ng.git/tree/docs/uClibc_vs_SuSv3.txt?h=v1.0.29#n104

Unimplemented mathematical interfaces:
--------------------------------------
math.h: [126] many
complex.h: [46] all, except cabs
fenv.h: [11] all

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2017-06-27
    • 2015-03-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多