Вопрос

I have a problem with the #define in C I am using MSP430F5418 with IAR EW 5.10 I have a pragma called location which will put the next declaring variable to the specified segment. In the below example a will put into the segment called myseg and b is not.

 #pragma location="myseg"
 static const char a[] = "amma";
 static const char b[] = "amrita";

I have a lot of constants like this. I want to know whether I could do something like this...

#define TYPE location="myseg" \
       static const char 
#pragma TYPE a = "amma";
#pragma TYPE b = "amrita";
.....

so that I can avoid #pragma location="myseg" before each variable declaration.

Это было полезно?

Решение

You cannot use a #pragma inside a #define, nor the other way round.

To circumvent this restriction, some compilers offer a _Pragma operator (GCC, LLVM) (__pragma in Visual C++) which provide the same functionality as the #pragma directive. This operator can be used in another macro. Find out whether your compiler supports such a pragma operator.

Using this, you could write:

#define DECLARE_IN_SEG(decl) \
    _Pragma(location="myseg") \
    static const char decl;

DECLARE_IN_SEG(a = "amma");
DECLARE_IN_SEG(b = "amrita");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top