Forced built-in function in another translation unit

This part of the gcc manual is rather obscure, and I cannot understand the use of the forceinline attribute after retrying.

I define an object and some functions for managing this object. Few of these functions can use atomic instructions, and I want the compiler to inline these functions. However, I do not want to write these functions in the header file and declare them with "static inline", as in the linux kernel.

Is there a way to force gcc to inline functions from another translation unit?

+8
c gcc inline
Nov 05 '12 at 8:23
source share
1 answer

you can use the always_inline attribute, for example:

 void foo () __attribute__((always_inline)); 

From docs

always_inline Functions are generally not built-in unless optimization is specified. For functions declared inline, this attribute even if the optimization level is not specified.

Note1 : there is no need to use inline if you use the always_inline attribute

Note 2 . If the function cannot be inlined, you will receive a warning if, for example, the definition is not available at compilation, with higher optimization gcc can still embed it in the caller, there is a special switch for this:

 -funit-at-a-time 

From docs :

The optimization levels of -O2 and higher, in particular, allow the unit-at-time mode, which allows the compiler to view information obtained from later functions in a file when compiling a function . Compilation of several files at once into one output file in unit-at-time mode allows the compiler to use the information obtained from all files when compiling each of them .

Note3 : There is no need to have an explicit prototype so you can use the attribute for the defintion function:

 __attribute__((always_inline)) void foo() { //some code } 

Also see this discussion , it answers some of your questions.

+9
Nov 05 '12 at 8:25
source share



All Articles