| |||
| Home > Coding Practices > Function inlining > Marking functions as static | |||
At the optimization levels -O2 and -O3,
the compiler is able to automatically inline a function if it is
practical to do so, even when the function is not declared as __inline or inline.
To control the automatic inlining of functions at higher optimization
levels, use the --no_autoinline and --autoinline command-line
options.
Unless a function is explicitly declared as static (or __inline),
the compiler has to retain the out-of-line version of it in the
object file in case it is called from some other module. The linker
is unable to remove unused out-of-line functions from an object unless
you place them in their own sections using one of the following
methods:
--split_sections in the Compiler Reference Guide
__attribute__((section("name"))) in the Compiler Reference Guide
#pragma arm section [section_sort_list] in the Compiler Reference Guide
linker feedback.
If you fail to declare functions that are never called from outside a module as static, your code can be adversely affected. In particular, you might have:
A larger code size, because out-of-line versions of functions are retained in the image.
When a function is automatically inlined, both the in-line version and an out-of-line version of the function might end up in the final image, unless the function is declared as static. This might possibly increase code size.
An unnecessarily complicated debug view, because there are both inline versions and out-of-line versions of functions to display.
Retaining both inline and out-of-line copies of a function in code can sometimes be confusing when setting breakpoints or single-stepping in a debug view. The debugger has to display both in-line and out-of-line versions in its interleaved source view, so that you can see what is happening when stepping through either the in-line or out-of-line version.
Because of these problems, declare non-inline functions as static when you are sure that they can never be called from another module.