为什么我必须显式链接 libm?
可能的重复:
为什么必须链接数学库C?
当我编写一个使用 math.h
库中的函数的程序时,为什么我必须显式链接到 libm
即使它们是C 标准库的一部分?
例如,当我想使用 sin()
函数时,我需要 #include
但我还需要传递 -lm< /代码> 到海湾合作委员会。但对于标准库中的任何其他库,我不必这样做。为什么有区别?
Possible Duplicate:
Why do you have to link the math library in C?
When I write a program that uses functions from the math.h
library, why is it that I have to explicitly link to libm
even though they are part of the C standard library?
For instance, when I want to use the sin()
function I need to #include <math.h>
but I also need to pass -lm
to GCC. But for any other library from the standard library, I don't have to do that. Why the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在过去,链接器速度很慢,将大部分未使用的数学代码与其余代码分开可以使编译过程变得更快。今天的差异并不是很大,因此您可以将
-lm
选项添加到默认编译器配置中。请注意,标头
(或任何其他标头)不包含代码。它包含有关代码的信息,特别是如何调用函数。代码本身位于库中。我的意思是,您的程序不使用“
库”,它使用数学库并使用<;中声明的原型。 math.h>
标头。In the old days, linkers were slow and separating the mostly unused math code from the rest made the compilation process go faster. The difference is not so great today, so you can add the
-lm
option to your default compiler configuration.Note that the header
<math.h>
(or any other header) does not contain code. It contains information about the code, specifically how to call functions. The code itself is in a library. I mean, your program does not use the "<math.h>
library", it uses the math library and uses the prototypes declared in the<math.h>
header.这与您必须在大多数实现上显式链接到
libpthread
的原因相同。当一些新的和可怕的东西被添加到标准库中时,它通常首先作为一个单独的附加库实现,该库用符合新要求的版本覆盖旧标准库实现中的一些符号,同时还添加了大量新的界面。如果某些历史实现在libm
中具有用于浮点打印的单独版本的printf
,并且在主libc< 中具有“轻型”版本,我不会感到惊讶/code> 缺少浮点数。如果我没记错的话,ISO C 基本原理文档中实际上提到并鼓励微型系统采用这种实现方式。
当然,从长远来看,像这样将标准库分离出来会导致更多的问题而不是好处。最糟糕的部分可能是动态链接程序的加载时间和内存使用量增加。
It's the same reason you have to explicitly link to
libpthread
on most implementations. When something new and scary is added to the standard library, it usually first gets implemented as a separate add-on library that overrides some of the symbols in the old standard library implementation with versions that conform to the new requirements, while also adding lots of new interfaces. I wouldn't be surprised if some historical implementations had separate versions ofprintf
inlibm
for floating point printing, with a "light" version in the mainlibc
lacking floating point. This kind of implementation is actually mentioned and encouraged for tiny systems in the ISO C rationale document, if I remember correctly.Of course in the long-term, separating the standard library out like this leads to a lot more problems than benefits. The worst part is probably the increased load time and memory usage for dynamic-linked programs.
实际上,对于大多数数学函数,您通常不需要链接 libm 的原因是这些函数是由编译器内联的。如果不是这种情况,您的程序将无法链接到平台上。
Actually, the reason you normally don't need to link against libm for most math functions is that these are inlined by your compiler. Your program would fail to link on a platform where this is not the case.