#pragma init

Category

Object code control

Purpose

Specifies the order in which the runtime library calls a list of functions before main() is called.

For shared libraries, the init functions are called when the shared library is loaded to memory. For example, when using dynamic loading, this happens at the point when dlopen() is called.

Syntax

Read syntax diagramSkip visual syntax diagram
                       .-,-------------.      
                       V               |      
>>-#--pragma--init--(----function_name-+--)--------------------><

Usage

Any function that is specified in the pragma should have return type void (for example, void fA(); ) and take no parameters. Functions that have a non-void return type are accepted but the return value is discarded.

Functions that take parameters are ignored with a warning since the parameters would contain garbage values.

Within the same compilation unit, the list of functions in pragma init are called in the order specified. Similarly, within the same compilation unit, functions specified in more than one pragma init are called in the order in which the pragmas are encountered in the source.

In general, the order of static initialization across files and across libraries is nonstandard and therefore, a non-portable behavior. It is not advisable to build any dependency on this behavior. The order of functions across files is undefined, even when using the -Wm option).

Related information