Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force inline function in other translation unit

Tags:

c

gcc

inline

This part of the gcc manual is pretty obscure and i can't understand the usage of the forceinline attribute after repeated attempts.

I'm defining an object and certain functions to manipulate that object. Few of those functions can use atomic instructions and i want the compiler to inline those functions. However i do not want to write those functions in the header file and declare them with "static inline" like in the linux kernel.

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

like image 288
sgupta Avatar asked Nov 05 '12 08:11

sgupta


People also ask

Can a function be forced as inline?

There's no guarantee that functions will be inlined. You can't force the compiler to inline a particular function, even with the __forceinline keyword.

What are the restriction of inline function?

Limitations of Inline FunctionsInline functions do not work if the body of the function contains any sort of looping or iteration. Inline functions do not support the use of switch or goto statements. C++ Inline functions cannot work if the function defined is recursive in nature.

How is inline function defined outside the body of a class?

An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (and define the function outside of its class) or to define it outside of the class declaration using the inline keyword.

Is inline deprecated?

Inline classes are a subset of value-based classes. They don't have an identity and can only hold values. The inline modifier for inline classes is deprecated.


1 Answers

you can use the always_inline attribute, for example:

void foo () __attribute__((always_inline));

From the docs

always_inline Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level was specified.

Note1: There's no need to use inline if you use the always_inline attribute

Note2: If the function could not be inlined you will get a warning, if for example the definition is not available when compiling, however, at a higher optimization gcc can still inline it into the caller, there's a specific switch for that too:

-funit-at-a-time

From the docs:

Optimization levels -O2 and above, in particular, enable unit-at-a-time mode, which allows the compiler to consider information gained from later functions in the file when compiling a function. Compiling multiple files at once to a single output file in unit-at-a-time mode allows the compiler to use information gained from all of the files when compiling each of them.

Note3: It is not necessary to have an explicit prototype so you can use the attribute on the function defintion:

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

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

like image 137
iabdalkader Avatar answered Nov 17 '22 01:11

iabdalkader