2

In gcc, it appears that referencing the results of a macro expansion later inside that same expansion doesn't work. For example:

#define TESTMACRO(name) \
        static int name##_func(int solNo) { \
                return (solNo); \
        }\
        static int name##Thing = {0,##name##_func},NULL,{"", capInvSw##name}}; 

TESTMACRO(stuff)

This results in errors like this:

test.c:7:29: error: pasting "," and "stuff" does not give a valid preprocessing token
  static int name##Thing = {0,##name##_func},NULL,{"", capInvSw##name`}}; 
                             ^
test.c:9:1: note: in expansion of macro ‘TESTMACRO’
 TESTMACRO(stuff)

I would expect to have a function called stuff_func created and passed into stuffThing. I believe that this works in other compilers. What is the equivalent way to do this in gcc?

2
  • What does your pre-processor output present for the expansion? Commented Mar 7, 2019 at 22:19
  • 1
    Looks like you have a spurious extra ## in the macro body, which is what the error message is complaining about... Commented Mar 8, 2019 at 0:28

1 Answer 1

1

You can try to run only the pre-processor on your code by passing the -E flag:

gcc -E foo.c

Which evaluates your macro to:

static int stuff_func(int solNo) { return (solNo); } static int stuffThing = {0,stuff_func},NULL,{"", capInvSwstuff`}};

That can be expanded for readability to:

static int stuff_func(int solNo) { 
return (solNo); 
} 

static int stuffThing = {0,stuff_func},NULL,{"", capInvSwstuff`}};

And it appears that you have one extra/missing brackets } in your expanded macro.

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, yes, this was an artificial example meant to strip some ambiguity from a real life pinmame issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.