I have a function called create_car. I am trying to use a preprocessor macro so that I can call the function by doing Create(car). Here is what I have thus far:
#define ObjName(X) _Generic((X), struct Person: "person", struct Car: "car")
#define NewObj(X) create_ ## ObjName(X)
struct Car* create_car(void)
{
// do something here
}
struct Car {
char* make;
char* model;
int year;
void (*print)(struct Car*);
};
int main(void)
{
struct Car *car = NewObj(struct Car);
}
But when trying that I get the following error:
gen.c:5:21: warning: implicit declaration of function ‘create_ObjName’; did you mean ‘create_car’? [-Wimplicit-function-declaration]
#define NewObj(X) create_ ## ObjName(X)
^
gen.c:40:5: note: in expansion of macro ‘NewObj’
NewObj(struct Car);
^~~~~~
gen.c:40:12: error: expected expression before ‘struct’
NewObj(struct Car);
^
gen.c:5:40: note: in definition of macro ‘NewObj’
#define NewObj(X) create_ ## ObjName(X)
What does the issue seem above with how I'm trying to create the object from the macro?
What I'm looking the code to become after preprocessed/compiled is:
int main(void)
{
struct Car *car = create_car();
}