Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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) {\

have your tried outputting the code after the preprocessing. perhaps via the '-E' option in gcc – user3629249 Sep 27, 2019 at 4:44 @bostonbrooks: i guess you'll need to expand it all out. I'm not sure what you are really trying to do. – rici Sep 26, 2019 at 3:04 @BostonBrooks I'd advise to place all code generation completely outside your C source. Make a separate program for that. Because these kind of icky macros have about 99% chance of backfiring. – Lundin Sep 26, 2019 at 10:49

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.