inline/static/extern

1.inline是一个编译优化选项,需要满足一定的条件才会达到优化的目的,即直接在调用时展开,而省去调用函数的开销。对于gcc 是-O。

2.static inline表明这个函数只在本编译单元中可见。inline或者extern inline都意味着此函数是全局可见的;不同的是inline限定的函数在定义的编译单元中被直接展开,而在其它调用关系的编译单元中被直接函数调用, 而extern inline限定的函数在所有的调用中都被直接展开,相当于宏展开,强行的函数调用无效,除非定义一个非extern inline限定的版本放在库中以供调用,所以这个extern表明这只是个声明, 如linux-0.1x版本中的strcpy函数, 在include/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;
}
在lib/string.c中, 将extern和inline定义为空, 并再次包含了include/string.h, 相当于定义了一个非extern, 非inline的函数:
#define extern
#define inline
#define __LIBRARY__
#include <string.h>

作者: tfengjun   发布时间: 2010-10-29