çpre处理器定义生成的函数名 [英] C pre-processor defining for generated function names

查看:123
本文介绍了çpre处理器定义生成的函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我有好几个生成的函数,并愿在,我已经创造了一些通用的功能指向他们(让我再利用基地code时生成的函数名称更改)。

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

一旦这些产生,我想定义一个宏,让他们被总称为。我的想法是这样,但随着 C pre处理器采用宏观的,而不是什么宏定义为的名字我没有任何运气实现它...

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;
}

问题是,我收到

void Callback_SIGNAL1(void)

而不是

void Callback_SignalName1(void)

有没有解决这个好办法?

Is there a good way around this?

推荐答案

您需要提供函数宏额外的水平,以确保正确的扩展:

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

例如

#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;
}

输出:

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







void Callback_SignalName1 (void)
{
 return;
}

这篇关于çpre处理器定义生成的函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆