为什么即使在包含 math.h 并使用 -lm 链接到数学库之后我仍然得到 "Undefined symbol: .sqrtf"
why I am getting "Undefined symbol: .sqrtf" even after Including math.h and linking to math library using -lm
当我尝试编译时
#include <math.h>
#include <stdio.h>
int main()
{
double m=1.66666;
double k=sqrtf(m);
return 0;
}
使用以下命令
/user/unicore/rs6000aix/compiler/gcc4.8.5/aix6.1/bin/gcc -o test.out test.cpp -lm
它抛出
ld: 0711-317 ERROR: Undefined symbol: .sqrtf
ld: 0711-345 使用-bloadmap 或-bnoquiet 选项获取更多信息。
collect2:错误:ld 返回 8 退出状态
但是下面的代码编译成功
#include <math.h>
#include <stdio.h>
int main()
{
double k=sqrtf(1.66666);
return 0;
}
我正在使用 gcc4.8.5 编译代码。相同的代码在 AIX6.1 上编译成功,但在新机器 (AIX7.1) 上编译失败
类似的问题已经存在:Why am I getting "undefined reference to sqrt" error even though I include math.h header? 但它对我不起作用。
更新:当我使用 sqrt
而不是 sqrtf
代码编译成功时,使用命令 '/user/unicore/rs6000aix/compiler/gcc4.8.5/aix6.1/bin/gcc -o test.out test.cpp -lm to compile it.
sqrtf` 失败,无论是否链接到数学库。
edit2:nm 命令的输出
$ nm -g -X32 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4196 12
$ nm -g -X64 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4296 24
编辑 3:安装了 bos.adt.libm.7.1.3.47
,但没有 sqrtf
。安装 bos.adt.libm.7.1.4.30.bff
并开始正常工作。
安装包 bos.adt.libm
。或者让你的系统管理员来做。
--
编辑:您的代码中对函数 sqrtf
的调用可能会被优化掉,因为它可以在编译时计算,而且永远不会使用结果。这是一个实际的例子:
#include <math.h>
#include <stdio.h>
int main (int argc,char **argv)
{
double val, k;
if (argc>1) val= strtod (argv[1], NULL);
else val= 1.66666;
k=sqrtf (val);
printf ("val=%g k=%g\n", val, k);
return 0;
}
编译:
gcc -o sid_math sid_math.c -Wl,-bmap:sid_math.map # fails
gcc -o sid_math sid_math.c -lm -Wl,-bmap:sid_math.map # works
--
编辑:您还应该检查 libma.a
的内容,例如:
$ nm -g -X32 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4132 12
.sqrtf T 480
sqrtf D 6364 12
$ nm -g -X64 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4232 24
.sqrtf T 480
sqrtf D 6616 24
当我尝试编译时
#include <math.h>
#include <stdio.h>
int main()
{
double m=1.66666;
double k=sqrtf(m);
return 0;
}
使用以下命令
/user/unicore/rs6000aix/compiler/gcc4.8.5/aix6.1/bin/gcc -o test.out test.cpp -lm
它抛出
ld: 0711-317 ERROR: Undefined symbol: .sqrtf
ld: 0711-345 使用-bloadmap 或-bnoquiet 选项获取更多信息。 collect2:错误:ld 返回 8 退出状态
但是下面的代码编译成功
#include <math.h>
#include <stdio.h>
int main()
{
double k=sqrtf(1.66666);
return 0;
}
我正在使用 gcc4.8.5 编译代码。相同的代码在 AIX6.1 上编译成功,但在新机器 (AIX7.1) 上编译失败
类似的问题已经存在:Why am I getting "undefined reference to sqrt" error even though I include math.h header? 但它对我不起作用。
更新:当我使用 sqrt
而不是 sqrtf
代码编译成功时,使用命令 '/user/unicore/rs6000aix/compiler/gcc4.8.5/aix6.1/bin/gcc -o test.out test.cpp -lm to compile it.
sqrtf` 失败,无论是否链接到数学库。
edit2:nm 命令的输出
$ nm -g -X32 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4196 12
$ nm -g -X64 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4296 24
编辑 3:安装了 bos.adt.libm.7.1.3.47
,但没有 sqrtf
。安装 bos.adt.libm.7.1.4.30.bff
并开始正常工作。
安装包 bos.adt.libm
。或者让你的系统管理员来做。
--
编辑:您的代码中对函数 sqrtf
的调用可能会被优化掉,因为它可以在编译时计算,而且永远不会使用结果。这是一个实际的例子:
#include <math.h>
#include <stdio.h>
int main (int argc,char **argv)
{
double val, k;
if (argc>1) val= strtod (argv[1], NULL);
else val= 1.66666;
k=sqrtf (val);
printf ("val=%g k=%g\n", val, k);
return 0;
}
编译:
gcc -o sid_math sid_math.c -Wl,-bmap:sid_math.map # fails
gcc -o sid_math sid_math.c -lm -Wl,-bmap:sid_math.map # works
--
编辑:您还应该检查 libma.a
的内容,例如:
$ nm -g -X32 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4132 12
.sqrtf T 480
sqrtf D 6364 12
$ nm -g -X64 /usr/lib/libm.a | grep sqrtf
.csqrtf T 512
csqrtf D 4232 24
.sqrtf T 480
sqrtf D 6616 24