__attribute__((constructor[(priority)])) function attribute
This attribute causes the function it is associated with to be called automatically before main()
is entered.
Syntax
__attribute__((constructor[(
)]))
priority
Where
is an optional
integer value denoting the priority. A constructor with a low integer
value runs before a constructor with a high integer value. A constructor
with a priority runs before a constructor without a priority.priority
Priority values up to and including 100
are
reserved for internal use. If you use these values, the compiler
gives a warning. Priority values above 100
are
not reserved.
Usage
You can use this attribute for start-up or initialization code. For example, to specify a function that is to be called when a DLL is loaded.
This attribute can be preferable to the linker option --init=
if you are using
GNU makefiles unmodified to build with the ARM compiler. That is, if you are using symbol
--translate_gcc
, --translate_gld
, or --translate_g++
.
Example
In the following example, the constructor functions are called
before execution enters main()
, in the order
specified:
int my_constructor(void) __attribute__((constructor)); int my_constructor2(void) __attribute__((constructor(101))); int my_constructor3(void) __attribute__((constructor(102))); int my_constructor(void) /* This is the 3rd constructor */ { /* function to be called */ ... return 0; } int my_constructor2(void) /* This is the 1st constructor */ { /* function to be called */ ... return 0; } int my_constructor3(void) /* This is the 2nd constructor */ { /* function to be called */ ... return 0; }