Search code examples
cmacroscompiler-constructionglobal-variables

how to place a global variable in another section


I was wondering if there is a macros that I can use to place a global variable in another section. i.e my_array would be stored in bss. since it's uninitialized but I want to place it in .data section instead.

uint8_t my_array[]; 

int main()
{
  return 1;
}

Solution

  • If you're using gcc, you can use the __section__ attribute to set the section:

    __attribute__((__section__(".data")))
    uint8_t my_array[5];
    

    Note that you'll also need to set a size for it, otherwise you only have a declaration instead of a definition.