libc 如何提供具有两个名称的函数?
在直接绑定(-B direct)出现之前libc提供了许多 有两个名称的函数。例如,getpwent() 和 _getpwent()。 这两个名称引用了 libc 中完全相同的函数。
libc如何让两个函数名指向同一个实现?
我认为这不应该像编写两次相同的代码那么容易。
Before the advent of direct binding (-B direct) libc provided many
functions with two names. For example, getpwent() and _getpwent().
These two names referred to exactly the same function in libc.
How does libc make two function names point to the same implementation?
I think it should not be as easy as writing the same code twice though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是通过弱别名完成的,这是一种“非标准”链接器技巧,自早期的unices以来就一直存在,并且我所知道的所有unix编译器/链接器都支持它。它基本上是这样完成的:
通常使用宏来稍微抽象一下。这使得符号
foo
默认情况下与符号__foo
具有相同的地址和类型,但允许它被其他地方的“强”定义覆盖。It's done via weak aliases a "nonstandard" linker trick that's been around since early unices and that's supported by all unix compilers/linkers I know of. It's basically done as:
often with macros to abstract it a little bit. This makes it so the symbol
foo
will have the same address and type as the symbol__foo
by default, but allows it to be overridden by a "strong" definition somewhere else.getpwent() 实现只是简单地调用 _getpwent() 。这样做的原因是为了隐藏函数调用的某些功能,并避免所谓的名称空间污染。通过这种方式,您可以创建一种抽象,允许您向用户隐藏某些内容。此外,前导下划线和双下划线是系统保留的,并且是备份,以确保您不会覆盖宏定义中的某些内容。
getpwent() implementation just calls _getpwent() simple as that. The reason this is done is to hide some functionality from function calls and to avoid something called namespace pollution. This way you can create a sort of abstraction that allows you to hide things from the user. Also leading underscore and double underscore are system reserved and are backups to make sure that you don't override something such as in macro definitions.