Is it possible to include a pre-defined id in an identifier?
The header:
/* myfile.h */
#define func MYID##_func
static void func() {}
#undef func
The c file:
/* myfile.c */
#define MYID FOO
#include "myfile.h"
#undef MYID
#define MYID BAR
#include "myfile.h"
#undef MYID
/* check the functions exist */
void main()
{
FOO_func();
BAR_func();
}
My aim here is to create 2 functions: (FOO_func
, BAR_func
).
I tried to compile the code above, and the functions are identified as MYID_func
(which fails since there are 2 of them).
I could do:
#define func FOO_func
#include "myfile.h"
#undef func
... But this doesn't scale so nicely when the file has many identifiers, since they would all have to be defined first.
The goal is to be able to avoid naming collisions when the file is included multiple times in the same C file.
You can use couple of layers of macros to accomplish what you want to.
You need the extra layers to make sure that
MYID
is expanded as a macro.