覆盖C库函数,调用原 [英] Overriding C library functions, calling original

查看:104
本文介绍了覆盖C库函数,调用原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑如何以及为什么code ++工程,因为它没有。在我的工作任何项目我没有真正遇到过这一点,我也没有想到这样做我自己。

override_getline.c:

 的#include<&stdio.h中GT;#定义OVERRIDE_GETLINE#IFDEF OVERRIDE_GETLINE
ssiz​​e_t供函数getline(字符** lineptr,为size_t * N,FILE *流)
{
    输出(函数getline&安培; lineptr =%P和; N =%P和;流=%P \\ N,lineptr,N,流);
    返回-1; //注意:错误号已不定值
}
#万一

main.c中:

 的#include<&stdio.h中GT;诠释的main()
{
    字符* BUF = NULL;
    为size_t LEN = 0;
    的printf(的Hello World%ZD \\ n!,函数getline(安培; BUF,和放大器; LEN,标准输入));
    返回0;
}

最后,例如编译和运行命令:

  GCC的main.c override_getline.c&放大器;&安培; ./a.out

随着 OVERRIDE_GETLINE 定义,自定义函数被调用,如果它被注释掉,正常的库函数被调用,并如预期都工作。

问题


  1. 什么是这个正确的称呼? 覆盖,阴影,别的东西?


  2. 这是GCC特有的,或POSIX,或ANSI C,甚至所有未定义的?


  3. 这有什么区别,如果功能是ANSI C函数或(如这里)POSIX的功能?


  4. 在什么地方覆盖函数被调用?通过在同一个连接其他的.o 的文件,至少,我presume .A 文件添加到链接命令了。如何用 -l <​​/ code>添加静态或动态链接库的命令行选项?


  5. 如果有可能,我怎么调用函数getline的库版本从重写函数getline?



解决方案

链接器将搜索你在命令行上的符号提供第一,它在图书馆查找之前的文件。这只要它看到函数getline 已定义,它将不再寻找另一个函数getline 符号表示。这是怎样的接头适用于所有平台。

这当然具有影响你的第五点,在没有可能叫原始函数getline ,因为你的函数的原但从接头的点

有关第五点,你可能想看看例如这个老答案

I am a bit puzzled on how and why this code works as it does. I have not actually encountered this in any project I've worked on, and I have not even thought of doing it myself.

override_getline.c:

#include <stdio.h>

#define OVERRIDE_GETLINE

#ifdef OVERRIDE_GETLINE
ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
    printf("getline &lineptr=%p &n=%p &stream=%p\n", lineptr, n, stream);
    return -1; // note: errno has undefined value
}
#endif

main.c:

#include <stdio.h>

int main()
{
    char *buf = NULL;
    size_t len = 0;
    printf("Hello World! %zd\n", getline(&buf, &len, stdin));
    return 0;
}

And finally, example compile and run command:

gcc main.c override_getline.c && ./a.out

With the OVERRIDE_GETLINE define, the custom function gets called, and if it is commented out, normal library function gets called, and both work as expected.

Questions

  1. What is the correct term for this? "Overriding", "shadowing", something else?

  2. Is this gcc-specific, or POSIX, or ANSI C, or even undefined in all?

  3. Does it make any difference if function is ANSI C function or (like here) a POSIX function?

  4. Where does the overriding function get called? By other .o files in the same linking, at least, and I presume .a files added to link command too. How about static or dynamic libs added with -l command line option of linker?

  5. If it is possible, how do I call the library version of getline from the overriden getline?

解决方案

The linker will search the files you provide on the command line first for symbols, before it searches in libraries. This means that as soon as it sees that getline has been defined, it will no longer look for another getline symbol. This is how linkers works on all platforms.

This of course has implications for your fifth point, in that there is no possibility to call the "original" getline, as your function is the original from the point of view of the linker.

For the fifth point, you may want to look at e.g. this old answer.

这篇关于覆盖C库函数,调用原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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