Inlining of functions is an optimization and it really “works” only in optimizing compilation. If you don't use -O, no function is really inline.
一个函数的inlining是一种优化,而它只会"work"在有优化选项的编译中。如果你没有(在GCC中)用到-O选项,任何函数都不会被inline。
When a function is both inline and static, if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option -fkeep-inline-functions. Some calls cannot be integrated for various reasons (in particular, calls that precede the function's definition cannot be integrated, and neither can recursive calls within the definition). If there is a nonintegrated call, then the function is compiled to assembler code as usual. The function must also be compiled as usual if the program refers to its address,because that can't be inlined.
当一个函数同时是inline和static的,如果对这个函数的所有调用都可以直接在调用函数中展开,而且此函数的地址没有被引用(我的理解是函数指针调用),即这个函数的汇编代码没有被引用。这种情况下,GCC实际上不生成此函数的汇编代码,除非你指定-fkeep-inline-functions选项。某些条件下有些函数调用不能直接展开(比如,在函数定义之前的函数调用,再如函数定义中有递归)。如果有没有被直接展开的函数调用,此函数会按例生成汇编代码。当程序引用了此函数的地址时,这个函数也不能直接在调用中展开,因此也按例被编译。
When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.
当一个inline函数不是static,编译器会假定它会被其它编译单元调用,因为一个全局符号在一个程序中只能定义一次,所以这个函数不会在其他编译单元中被定义,所以那里的函数调用就不能被直接展开(注:只有在定义的文件中函数调用才会展开)。因此,非static的inline函数都会按例被编译。
If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.
当一个函数是inline和extern的,这个函数定义只会在调用中展开,而不会被编译,即使你明确引用它的地址。这个地址会成为一个外部引用,就好像你只是声明了这个函数,却没有定义它。
This combination of inline and extern has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline and extern) in a library file. The definition in the header file will cause most calls to the function to be inlined. If any uses of the function remain, they will refer to the single copy in the library.
inline和extern的函数基本上和一个宏定义的作用相当。使用的方法是在头文件中用这两个关键字定义,而把另外一份相同定义却没有inline和extern关键字的copy放在库中。头文件中的定义会使大部分的函数调用直接展开。如果还存在(通常开销)函数的调用,它就会使用库中的定义。