-1

I'm trying to recreate this line via the preprocessor

#pragma location = ADDR + OFFSET

Currently, i'm pretty close by using this (C Preprocessor, Stringify the result of a macro):

#define QUOTE(str) #str
#define EXPAND_AND_QUOTE(str) QUOTE(str)
#define TEST(X) location  =  ## X ##   + OFFSET
#define TESTE(X) EXPAND_AND_QUOTE(TEST(X))

_Pragma(TESTE(ADDR));

Which works if i remove the = and + sign from

#define TEST(x) location = ## X ## + OFFSET

Unfortunately, adding the = or + sign provides me with these errors:

test.c:3:27: error: pasting "=" and "ADDR" does not give a valid preprocessing token
 #define TEST(X) location  =  ## X ##   + OFFSET
                           ^
test.c:4:35: note: in expansion of macro ‘TEST’
 #define TESTE(X) EXPAND_AND_QUOTE(TEST(X))
                                   ^~~~
test.c:6:9: note: in expansion of macro ‘TESTE’
 _Pragma(TESTE(ADDR));
         ^~~~~
test.c:6:15: error: pasting "ADDR" and "+" does not give a valid preprocessing token
 _Pragma(TESTE(ADDR));
               ^
test.c:3:33: note: in definition of macro ‘TEST’
 #define TEST(X) location  =  ## X ##   + OFFSET
                                 ^
test.c:6:9: note: in expansion of macro ‘TESTE’
 _Pragma(TESTE(ADDR));
         ^~~~~

Any ideas? Thanks!

Jon V
  • 506
  • 1
  • 3
  • 21
  • 4
    Why are you trying to use token pasting in your macro definition? Just define it as `#define TEST(X) location = (X) + OFFSET` (note the parentheses for proper grouping in the event that `X` is an expression). – lurker Jun 30 '17 at 12:11

1 Answers1

3

You don't need to use ## operator:

#define QUOTE(str)  #str
#define TESTE(X)    QUOTE(location = X + OFFSET)

_Pragma(TESTE(ADDR));

Example on Ideone.

user694733
  • 15,208
  • 2
  • 42
  • 68