
最近在读 Linux 0.12 相关的书,发现源码里 /include/string.h 这样写:
#ifndef _STRING_H_ #define _STRING_H_ //省略... extern inline char * strcpy(char * dest,const char *src) { __sm__("cld\n" "1:\tlodsb\n\t" "stosb\n\t" "testb %%al,%%al\n\t" "jne 1b" ::"S" (src),"D" (dest):"si","di","ax"); return dest; } //省略... #endif 但在 /lib/strings.c 里,extern 和 inline 被定义成了空以后又使用了 /include/string.h :
/* * linux/lib/strings.c * * (C) 1991 Linus Torvalds */ #ifndef __GNUC__ #error I want gcc! #endif #define extern #define inline #define __LIBRARY__ #include <string.h> 书上写,这个做法可以尽量让 /include/string.h 里的 strcpy 被替换到其他函数里 (inline 起作用),然后让那些无法替换的部分调 /lib/strings.c 里的 strcpy 。
这个分流效果 (inline 可以起作用的地方就起作用,不起作用的地方调库里的函数) 是怎么做到的?
1 kkhaike 2023-03-11 15:34:05 +08:00 你说的“分流”应该就是 extern inline 关键字组合的意义。。 相关参考 https://blog.csdn.net/force_eagle/article/details/11106571 |
2 gridsah OP |
3 gridsah OP @kkhaike #1 感谢老哥发来的参考文章。现在已经搞清楚了。 我根据 GCC 的文档,还有 stackoverflow 上的内容,整理了一篇笔记出来,帮后来人排坑。 https://lishouzhong.com/article/wiki/coding/c&cpp/C%20%E8%AF%AD%E8%A8%80%20inline%20%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E8%A1%8C%E4%B8%BA.html |