我正在尝试测试 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.a
、libm.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/