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

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

问题描述

我有很多生成函数的情况,并希望将它们指向我创建的一些通用函数(以允许我在生成的函数名称更改时重用基本代码).

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 预处理器采用宏的名称而不是宏的定义:

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)

有什么好的办法吗?

推荐答案

需要额外提供一层类函数宏"来保证正确扩展:

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

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

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