29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Compiler</strong> Coding Practices<br />

5.26 Inline functions in C++ and C90 mode<br />

The inline keyword is not available in C90.<br />

The effect of __inline in C90, and __inline and inline in C++, is identical.<br />

When declaring an extern function to be inline, you must define it in every translation unit that<br />

it is used in. You must ensure that you use <strong>the</strong> same definition in each translation unit.<br />

The requirement of defining <strong>the</strong> function in every translation unit applies even though it has<br />

external linkage.<br />

If an inline function is used by more than one translation unit, its definition is typically placed<br />

in a header file.<br />

Placing definitions of non-inline functions in header files is not recommended, because this can<br />

result in <strong>the</strong> creation of a separate function in each translation unit. If <strong>the</strong> non-inline function is<br />

an extern function, this leads to duplicate symbols at link time. If <strong>the</strong> non-inline function is<br />

static, this can lead to unwanted code duplication.<br />

Member functions defined within a C++ structure, class, or union declaration, are implicitly<br />

inline. They are treated as if <strong>the</strong>y are declared with <strong>the</strong> inline or __inline keyword.<br />

Inline functions have extern linkage unless <strong>the</strong>y are explicitly declared static. If an inline<br />

function is declared to be static, any out-of-line copies of <strong>the</strong> function must be unique to <strong>the</strong>ir<br />

translation unit, so declaring an inline function to be static could lead to unwanted code<br />

duplication.<br />

The compiler generates a regular call to an out-of-line copy of a function when it cannot inline<br />

<strong>the</strong> function, and when it decides not to inline it.<br />

The requirement of defining a function in every translation unit it is used in means that <strong>the</strong><br />

compiler is not required to emit out-of-line copies of all extern inline functions. When <strong>the</strong><br />

compiler does emit out-of-line copies of an extern inline function, it uses Common Groups, so<br />

that <strong>the</strong> linker eliminates duplicates, keeping at most one copy in <strong>the</strong> same out-of-line function<br />

from different object files.<br />

5.26.1 See also<br />

Concepts<br />

<strong>Using</strong> <strong>the</strong> Linker:<br />

• Elimination of common groups or sections on page 5-3.<br />

Reference<br />

<strong>Compiler</strong> Reference:<br />

• __inline on page 5-7<br />

• --inline, --no_inline on page 3-53.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-37<br />

ID061811<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!