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

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

Note

This attribute is a GNU compiler extension supported by the ARM compiler.

Show/hideSyntax

__attribute__((constructor[(priority)]))

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.

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.

Show/hideUsage

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=symbol if you are using GNU makefiles unmodified to build with the ARM compiler. That is, if you are using --translate_gcc, --translate_gld, or --translate_g++.

Show/hideExample

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(102)));
int my_constructor3(void) __attribute__((constructor(101)));

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;
}

Show/hideSee also

Copyright © 2010-2012 ARM. All rights reserved.ARM DUI 0491H
Non-ConfidentialID070912