#define 内的#pragma
我正在使用C语言的微控制器工作。在这个特定的微控制器中,必须使用#pragma
按以下方式定义中断:
static void func();
#pragma INTERRUPT func <interrupt_address> <interrupt_category>
static void func() { /* function body */ }
是向量表中中断的地址。
为 1 或 2。例如,要在端口 0 引脚 0 中定义中断:
static void _int_p00();
#pragma INTERRUPT _int_p00 0x10 1
static void _int_p00() { (*isr_p00)(); }
我们在其他地方定义实际的中断服务例程并使用函数指针(如 isr_p00
code> 在示例中)来执行它们。
如果可以使用宏定义中断会很方便。我想按以下方式定义宏:
#define DECLARE_INTERRUPT(INT_NAME, INT_CAT) \
static void _int_##INT_NAME(); \
#pragma INTERRUPT _int_##INT_NAME INT_NAME##_ADDR INT_CAT \
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
编译器抛出以下错误:
Formal parameter missing after '#'
指示以下行:
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
我猜预处理器指令不能在#define中使用?有什么解决办法吗?
I'm working in a micro-controller using the C language. In this specific micro, the interrupts have to be defined using #pragma
in following way:
static void func();
#pragma INTERRUPT func <interrupt_address> <interrupt_category>
static void func() { /* function body */ }
The <interrupt_address>
is address of the interrupt in vector table. The <interrupt_category>
is either 1 or 2. For example, to define an interrupt in Port 0 pin 0:
static void _int_p00();
#pragma INTERRUPT _int_p00 0x10 1
static void _int_p00() { (*isr_p00)(); }
We define actual interrupt service routine elsewhere and use function pointer (like isr_p00
in the example) to execute them.
It would be convenient if the interrupts could be defined using a macro. I want do define a macro in following way:
#define DECLARE_INTERRUPT(INT_NAME, INT_CAT) \
static void _int_##INT_NAME(); \
#pragma INTERRUPT _int_##INT_NAME INT_NAME##_ADDR INT_CAT \
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
The compiler throwing the following error:
Formal parameter missing after '#'
indicating following line:
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
I guess preprocessor directives cannot be used in #define
s? Is there any work around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C99 具有新的
_Pragma
关键字,可让您将#pragma
放置在宏内。基本上,它需要一个字符串作为参数,该参数对应于您要提供给#pragma
指令的文本。如果您的编译器不支持此功能(gcc 支持)并且您需要寻求所需内容的外部实现(如上所述,
m4
可能是一个选择),那么最好的选择可能是保留为尽可能接近不太新的_Pragma
。然后,一旦您的编译器构建器赶上标准,您就可以停止使用脚本。C99 has the new
_Pragma
keyword that lets you place#pragma
inside macros. Basically it expects a string as an argument that corresponds to the text that you would have give to the#pragma
directive.If your compiler doesn't support this (gcc does) and you'd go for an external implementation of what you need (as said,
m4
could be a choice) the best would probably be to stay as close as possible to that not-so-new_Pragma
. Then once your compiler builder catches up with the standard you could just stop using your script.解决方法是使用代码生成或其他宏语言来预处理代码。
即用不同的扩展名编写代码。
让您的 makefile 或类似文件调用宏语言(例如 m4)或某种形式的脚本来生成 .c 文件,
然后对其进行编译。
A workround is to use code generation or another macro language to preprocess your code.
ie write the code with a different extension.
Have your makefile or similar call the macro language (e.g. m4) or a script of some form to generate a .c file
Then compile that.
据我所知,您具体要求的是不可能的。我假设预处理器的工作方式与 GNU C 预处理器 相同。在手册中,它指出:
As far as I'm aware, what you are specifically asking is impossible. I'm presuming a preprocessor that works the same as the GNU C Preprocessor. In the manual for that, it states: