2

I'm trying to write a macro that generates code for an object pool for any given class of objects in C. I keep getting error: '#' is not followed by a macro parameter whenever I run the preprocessor

I've tried replacing x##y with:

#define CONCAT1(x, y) x##y
#define CONCAT2(x, y) CONCAT1(x, y)

as this was suggested in a similar question

#define CONCAT1(x, y) x##y
#define CONCAT2(x, y) CONCAT1(x, y)

#define DECLARE_POOL(CLASS, LEVEL1, LEVEL2) {\
\
    #define CONCAT2(CLASS, _Pool_Level1) LEVEL1\
    #define CONCAT2(CLASS, _Pool_Level2) LEVEL2\
\
    CLASS* CONCAT2(CLASS, _Pool)[LEVEL1] = { 0 };\
\
    int CONCAT2(CLASS, _Available_Head) = -1;\
    int CONCAT2(CLASS, _Available_Tail) = -1;\
\
    int CONCAT2(CLASS, _In_Use_Head) = -1;\
    int CONCAT2(CLASS, _In_Use_Tail) = -1;\
\
    int CONCAT2(CLASS, _Increase_Pool)(int Level1_Address){\
\
    }\
\
    int CLASS(int Address) {\
\
    }\
\
    int CONCAT2(CLASS, _Delete)(int Address) {\
\
    }\
\
    int CONCAT2(CLASS, s)(int Address)\
\
    }\
\
    int CONCAT2(CLASS, _Save_All)(void)\
\
    }\
\
    int CONCAT2(CLASS, _Restore_All)(void)\
\
    }\
    int CONCAT2(CLASS, _Free_All)(void)\
\
    }\
}

Expected: Code goes through the preprocessor and gives function prototypes for objects of type "CLASS"
Actual Results:

error: '#' is not followed by a macro parameter
#define DECLARE_POOL(CLASS, LEVEL1, LEVEL2) {\

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
BostonBrooks
  • 105
  • 1
  • 6
  • Possible duplicate of [Macro-producing macros in C?](https://stackoverflow.com/questions/860273/macro-producing-macros-in-c) – walnut Sep 26 '19 at 02:55
  • have your tried outputting the code after the preprocessing. perhaps via the '-E' option in `gcc` – user3629249 Sep 27 '19 at 04:44

1 Answers1

4

You cannot use #define inside a macro. Or any other preprocessor directive, for that matter.

rici
  • 234,347
  • 28
  • 237
  • 341