9.34 __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.
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.


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++.


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;
Related reference
9.36 __attribute__((destructor[(priority)])) function attribute
Related information
--init=symbol linker option
Non-ConfidentialPDF file icon PDF versionARM DUI0375E
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.