c++cembeddedmisra

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?


Solution

  • 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")));