Skip to main content

IAR Embedded Workbench for Arm 9.70.x

Inlining functions

In this section:

Function inlining means that a function, whose definition is known at compile time, is integrated into the body of its caller to eliminate the overhead of the function call. This optimization, which is performed at optimization level High, normally reduces execution time, but might increase the code size. The resulting code might become more difficult to debug. Whether the inlining actually occurs is subject to the compiler’s heuristics.

The compiler heuristically decides which functions to inline. Different heuristics are used when optimizing for speed, size, or when balancing between size and speed. Normally, code size does not increase when optimizing for size.

C versus C++ semantics

In C++, all definitions of a specific inline function in separate translation units must be exactly the same. If the function is not inlined in one or more of the translation units, then one of the definitions from these translation units will be used as the function implementation.

In C, you must manually select one translation unit that includes the non-inlined version of an inline function. You do this by explicitly declaring the function as extern in that translation unit. If you declare the function as extern in more than one translation unit, the linker will issue a multiple definition error. In addition, in C, inline functions cannot refer to static variables or functions.

For example:

// In a header file.
static int sX;
inline void F(void)
{
  //static int sY; // Cannot refer to statics.
  //sX;            // Cannot refer to statics.
}

// In one source file.
// Declare this F as the non-inlined version to use.
extern inline void F();

Features controlling function inlining

There are several mechanisms for controlling function inlining:

  • The inline keyword.

    If you compile your function in C or C++ mode, the keyword will be interpreted according to its definition in Standard C or Standard C++, respectively.

    The main difference in semantics is that in Standard C you cannot (in general) simply supply an inline definition in a header file. You must supply an external definition in one of the compilation units, by designating the inline definition as being external in that compilation unit.

  • #pragma inline is similar to the inline keyword, but with the difference that the compiler always uses C++ inline semantics.

    By using the #pragma inline directive you can also disable the compiler’s heuristics to either force inlining or completely disable inlining. For more information, see inline.

  • ‑‑use_c++_inline forces the compiler to use C++ semantics when compiling a Standard C source code file.

  • ‑‑no_inline, #pragma optimize=no_inline, and #pragma inline=never all disable function inlining. By default, function inlining is enabled at optimization level High.

The compiler can only inline a function if the definition is known. Normally, this is restricted to the current translation unit. However, when the ‑‑mfc compiler option for multi-file compilation is used, the compiler can inline definitions from all translation units in the multi-file compilation unit. For more information, see Multi-file compilation units.

For more information about the function inlining optimization, see Function inlining.