Sun Studio 12 Update 1: C User's Guide

D.1.10 inline Functions

6.7.4 Function specifiers

Inline functions as defined by the 1999 C ISO standard are fully supported.

Note that according to the C standard, inline is only a suggestion to the C compiler. The C compiler can choose not to inline anything, and compile calls to the actual function.

The Sun Studio C compiler does not inline C function calls unless compiling at optimization level -xO3 or above, and only if the optimizer's heuristics decide it is profitable to do so. The C compiler does not provide a way to force a function to be inlined.

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above. If not profitable to inline (or at an optimization of less than -xO3), a reference to the actual function will be compiled and the function definition will be compiled into the object code. Note that if the program uses the address of the function, the actual function will be compiled in the object code and not inlined.

Extern inline functions are more complicated. There are two types of extern inline functions: an inline definition and an extern inline function.

An inline definition is a function defined with the keyword inline, without either the keywords static or extern, and with all prototypes appearing within the source (or included files) also containing the keyword inline without either the keywords static or extern. For an inline definition the compiler must not create a global definition of the function. That means any reference to an inline definition that is not inlined will be a reference to a global function defined elsewhere. Put another way, the object file produced by compiling this translation unit (source file) will not contain a global symbol for the inline definition. And any reference to the function that is not inlined will be to an extern (global) symbol provided by some other object file or library at link time.

An extern inline function is declared by a file scope declaration with the extern storage-class-specifier (that is, the function definition and/or prototype). For an extern inline function the compiler will provide a global definition of the function in the resulting object file. The compiler may choose to inline any references to that function seen in the translation unit (source file) where the function definition has been provided, or the compiler can choose to call the global function.

The behavior of any program that relies on whether or not a function call is actually inlined, is undefined.

Note also that an inline function with external linkage may not declare or reference a static variable anywhere in the translation-unit.

D.1.10.1 Sun C compiler gcc compatibility for inline functions

To obtain behavior from the Sun Studio C compiler that is compatible with gcc's implementation of extern inline functions for most programs, use the -features=no%extinl flag. When this flag is specified the Sun Studio C compiler will treat the function as if it were declared as a static inline function.

The one place this is not compatible will be when the address of the function is taken. With gcc this will be an address of a global function, and with Sun's compiler the local static definition address will be used.