需要使用-lm在Clang与pow(),但不是sqrt() [英] Need to use -lm in Clang with pow(), but not sqrt()
问题描述
在FreeBSD 10.1
并使用Clang版本 3.4.1
On FreeBSD 10.1
and using Clang version 3.4.1
现在我已经看到其他线程要求如何使用pow()编译,但我还没有看到有这个问题的线程。例如:
Now I have seen other threads asking how to compile with using pow(), but I haven't seen a thread with this question. Take for example:
#include <math.h>
#include <stdio.h>
int main()
{
float result;
result = pow(2,3);
printf("%d", result);
return 0;
}
现在使用 pow c>,我需要发出clang参数-lm。
Now using pow()
, I need to issue clang the argument -lm.
cc -lm -o name name.c
cc -lm -o name name.c
但是,用 sqrt(5);
替换 pow(2,3)
cc -o name name c
。如果他们都使用 math.h
,那么为什么 pow()
需要链接到库?
边不是:安装gcc来测试,我根本不需要使用-lm。
However, replacing pow(2,3)
with sqrt(5);
, I can compile with cc -o name name.c
. If they both use math.h
, then why does pow()
need to be linked to the library?
Side not: Installed gcc to test, and I don't need to use -lm at all.
推荐答案
您所定位的处理器的浮点硬件直接实现 sqrt
,与所有IEEE-754兼容单元一样。因此,编译器可以直接生成 sqrt
的代码,并且不需要链接到库函数。
You are targeting a processor whose floating point hardware implements sqrt
directly, as do all IEEE-754 compliant units. As such, the compiler can generate code for sqrt
directly and does not need to link to a library function.
另一方面,浮点硬件通常不提供 pow
的实现,因此它需要在图书馆中实施。
On the other hand, floating point hardware does not typically offer implementations of pow
and so it does need to be implemented in a library.
这篇关于需要使用-lm在Clang与pow(),但不是sqrt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!