为什么你需要数学库​​用C链接? [英] Why do you have to link the math library in C?

查看:91
本文介绍了为什么你需要数学库​​用C链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我包括<文件stdlib.h> < stdio.h中> 在C程序我没有编译时链接这些,但我确实有链接到<&math.h中GT; ,使用 -lm 用gcc,例如:

If I include <stdlib.h> or <stdio.h> in a C program I don't have to link these when compiling but I do have to link to <math.h>, using -lm with gcc, for example:

gcc test.c -o test -lm

是什么原因呢?我为什么要显式链接数学库而不是其他的库?

What is the reason for this? Why do I have to explicitly link the math library but not the other libraries?

推荐答案

在功能文件stdlib.h stdio.h中 libc.so 实现(或的libc.a 静态链接),它链接到您的在默认情况下的可执行文件(就像 -lc 中指定)。 GCC可以指示避免这种自动链接 -nostdlib -nodefaultlibs 选项。

The functions in stdlib.h and stdio.h have implementations in libc.so (or libc.a for static linking), which is linked into your executable by default (as if -lc were specified). GCC can be instructed to avoid this automatic link with the -nostdlib or -nodefaultlibs options.

文件math.h 数学函数在 libm.so 实现(或 libm.a 静态链接),而的libm 默认情况下不链接英寸这有的libm / 的libc 分裂历史的原因,他们没有非常有说服力的。

The math functions in math.h have implementations in libm.so (or libm.a for static linking), and libm is not linked in by default. There are historical reasons for this libm/libc split, none of them very convincing.

有趣的是,C ++运行时的libstdc ++ 要求的libm ,因此,如果您编译C ++程序与海湾合作委员会( G ++ ),您将自动获得的libm 的链接。

Interestingly, the C++ runtime libstdc++ requires libm, so if you compile a C++ program with GCC (g++), you will automatically get libm linked in.

这篇关于为什么你需要数学库​​用C链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆