user145760
user145760

Reputation: 134

Compatible declaration for __attribute__ ((section(".abc.dfe"))) const volatile uint8 attributeVariable = 0; - MISRA compliant

The following const is declared in a project file I'm testing for MISRA guidelines violations as

__attribute__ ((section(".abc.dfe"))) const volatile uint8 attributeVariable = 0;

MISRA test produces following message

A compatible declaration shall be visible when an object or function with external linkage is defined. 
Global definition of 'attributeVariable ' variable has no previous declaration.

I've already fixed other global definitions, which were not using the __attribute__ keyword by declaring it as

extern const volatile uint8 attributeVariable;

in the header file. I'm unsure if I can write the declaration in the header in the same way when using the __attribute__ keyword. Does the __attribute__ affect the way I should write extern declaration of the variable?

Upvotes: 0

Views: 600

Answers (2)

sgargoyle777
sgargoyle777

Reputation: 1

It's not like i expect you to have the same problem 5 years later, but for the future i'll add something to the conversation. I didn't find it anywhere but from testing i can assure you that with gcc you can drop the attribute section from declaration of an extern. From documentation it almost seems the opposite, as they always talk about "declaration".

It kinda make sense to me that the attribute about where to locate it in memory appears in the definition.

P.S. yeah it isn't standard C but assuming you are talking about MISRA:2012 it's clearly stated you can use extension of C if you prove you are using the right compiler :D

Upvotes: 0

Lundin
Lundin

Reputation: 214780

There's two problems here.

First of all MISRA-C demands that the code should be standard C, so you have to create a deviation from the rule about using standard C.

Second, MISRA-C doesn't like you to declare variables at file scope that aren't static. Global variables are frowned upon not only by MISRA-C, so ask yourself if you really must expose this variable all over the place, or if you can access it through setter/getter functions instead.

That being said, I believe__attribute__ works pretty much like other type qualifiers. You can write it in the beginning or the end of the declaration etc. So there should be no problem writing for example:

extern const volatile uint8_t attributeVariable __attribute__ ((section(".abc.dfe")));

Upvotes: 1

Related Questions