9

I'm working with some code that was originally written for IAR and converting it to compile using the GCC compiler.

However, I'm stuck with one particular line as I don't understand the syntax or what is going on.

__root const uint32_t part_number @ ".part_number" = 701052;

The __root I've found out is so that the the variable is included in the final code even if there is nothing that actually references it. const means that it won't change and is kept in the ROM instead of RAM.

It is the @ ".part_number" part that I don't follow. The specific error I get is "stray '@' in program".

I understand that @ isn't a part of standard C, but I haven't had any luck finding anything that explains this syntax I'm seeing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MidnightRover
  • 197
  • 1
  • 8
  • 1
    I suspect that it's an instruction for the linker to place that variable in the correspondingly named executable section. The gcc equivalent would be the `section` attribute (`const uint32_t part_number __attribute__ ((section (".part_number")) = 701052;`). – Matteo Italia Dec 12 '17 at 23:43
  • 2
    Not committing a full answer since I'm not certain, but the ".part_number" tells me it's probably telling the linker which section to store the variable/value in –  Dec 12 '17 at 23:43
  • Try out [this answer](https://stackoverflow.com/questions/24114365/does-at-symbol-and-dollar-sign-has-any-special-meaning-in-c-or-c). The 3rd answer suggests that it's a non-standard compiler extension for specifying a physical address – Psi Dec 12 '17 at 23:43
  • I agree with Matteo and Frank's comments. – Galen Dec 12 '17 at 23:44
  • Thanks everyone for your help! Really appreciated. – MidnightRover Dec 13 '17 at 16:11

1 Answers1

7

From this KB entry, it looks like it's syntax to instruct the linker to place the variable into a specific section:

If you instead place the object into a named segment:

__no_init struct setup located_configuration @ "SETUP";

The equivalent GCC syntax is through the section attribute.

const uint32_t part_number __attribute__ ((section (".part_number")) = 701052;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299