You copied the Doc URL to your clipboard.

__attribute__((constructor[(priority)])) function attribute

This attribute causes the function it is associated with to be called automatically before main() is entered.


This attribute is a GNU compiler extension that the ARM compiler supports.



Where priority 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.


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.


In the following example, the constructor functions are called before execution enters main(), in the order specified:

void my_constructor1(void) __attribute__((constructor));
void my_constructor2(void) __attribute__((constructor(102)));
void my_constructor3(void) __attribute__((constructor(103)));
void my_constructor1(void) /* This is the 3rd constructor */
{                        /* function to be called */
    printf("Called my_constructor1()\n");
void my_constructor2(void) /* This is the 1st constructor */
{                         /* function to be called */
    printf("Called my_constructor2()\n");
void my_constructor3(void) /* This is the 2nd constructor */
{                         /* function to be called */
    printf("Called my_constructor3()\n");
int main(void)
    printf("Called main()\n");

This example produces the following output:

Called my_constructor2()
Called my_constructor3()
Called my_constructor1()
Called main()

Related reference