gcc inline

 

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.
    當一個函數同時是inlinestatic的,如果對這個函數的所有調用都可以直接在調用函數中展開,而且此函數的地址沒有被引用(我的理解是函數指針調用),即這個函數的彙編代碼沒有被引用。這種情況下,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,編譯器會假定它會被其它編譯單元調用,因爲一個全局符號在一個程序中只能定義一次,所以這個函數不會在其他編譯單元中被定義,所以那裏的函數調用就不能被直接展開(注:只有在定義的文件中函數調用纔會展開)。因此,非staticinline函數都會按例被編譯。
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.
    當一個函數是inlineextern的,這個函數定義只會在調用中展開,而不會被編譯,即使你明確引用它的地址。這個地址會成爲一個外部引用,就好像你只是聲明瞭這個函數,卻沒有定義它。
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. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    inlineextern的函數基本上和一個宏定義的作用相當。使用的方法是在頭文件中用這兩個關鍵字定義,而把另外一份相同定義卻沒有inlineextern關鍵字的copy放在庫中。頭文件中的定義會使大部分的函數調用直接展開。如果還存在(通常開銷)函數的調用,它就會使用庫中的定義。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章