V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gridsah
V2EX  ›  C

求解 C 语言里的 extern 和 inline 被 define 定义成空时的疑问

  •  
  •   gridsah · 2023-03-11 15:02:06 +08:00 · 1221 次点击
    这是一个创建于 412 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在读 Linux 0.12 相关的书,发现源码里 /include/string.h 这样写:

    #ifndef _STRING_H_
    #define _STRING_H_
    
    //省略...
    
    extern inline char * strcpy(char * dest,const char *src)
    {
    __asm__("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 可以起作用的地方就起作用,不起作用的地方调库里的函数) 是怎么做到的?

    3 条回复    2023-03-17 21:30:32 +08:00
    kkhaike
        1
    kkhaike  
       2023-03-11 15:34:05 +08:00
    你说的“分流”应该就是 extern inline 关键字组合的意义。。
    相关参考 https://blog.csdn.net/force_eagle/article/details/11106571
    gridsah
        2
    gridsah  
    OP
       2023-03-11 21:16:53 +08:00   ❤️ 1
    @kkhaike 我在看 gnu89 和 gnu99 对 inline 关键字的处理方法,但是网上写的好乱....

    -----

    修正: /lib/strings.c -> /lib/string.c
    gridsah
        3
    gridsah  
    OP
       2023-03-17 21:30:32 +08:00   ❤️ 1
    @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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2847 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:10 · PVG 23:10 · LAX 08:10 · JFK 11:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.