Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

macros - C pre-processor defining for generated function names

I have a situation where I have quite a few generated functions, and would like to point them at some generic functions that I have created (to allow me to reuse the base code when the generated function names change).

Essentially, I have a list of function names as follows:

void Callback_SignalName1(void);
void Callback_SignalName2(void);
...etc

Once these are generated, I would like to define a macro to allow them to be called generically. My idea was this, but I haven't had any luck implementing it...as the C pre-processor takes the name of the macro instead of what the macro is defined as:

#define SIGNAL1 SignalName1
#define SIGNAL2 SignalName2

#define FUNCTION_NAME(signal) (void  Callback_ ## signal ## (void))
...
...
FUNCTION_NAME(SIGNAL1)
{
  ..
  return;
}

The issue is that I receive

void Callback_SIGNAL1(void)

instead of

void Callback_SignalName1(void)

Is there a good way around this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to provide an extra level of "function-like macro" to ensure the proper expansion:

e.g.

#define SIGNAL1 SignalName1
#define SIGNAL2 SignalName2

#define MAKE_FN_NAME(x) void  Callback_ ## x (void)
#define FUNCTION_NAME(signal) MAKE_FN_NAME(signal)

FUNCTION_NAME(SIGNAL1)
{
    return;
}

output:

$ gcc -E prepro.cc 
# 1 "prepro.cc"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "prepro.cc"







void Callback_SignalName1 (void)
{
 return;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...