简体   繁体   English

C元宏模式匹配

[英]C meta macro pattern matching

I have some macros KEY_* . 我有一些宏KEY_* I want to define them all as extern variables, but the number and name may vary. 我想将它们全部定义为外部变量,但数字和名称可能会有所不同。

file1.h: file1.h:

#define KEY_FOO 200
...
#define KEY_ASDFG 423

file2.c after preprocessor: 预处理程序后的file2.c:

#include <file1.h>
// Something like this should be generated by macros or so, but should not hardcode the names (except KEY_)
extern int key_foo; int key_foo = 200;
...
extern int key_asdfg; int key_asdfg = 423;

Can (and how) do I make a one meta macro/something that works like this and doesn't need to be called with FOO / ASDFG ? 我可以(以及如何)制作一个像这样工作并且不需要用FOO / ASDFG调用的meta宏吗?

You can't do this using only the C preprocessor. 您不能仅使用C预处理器来执行此操作。

At the least, it would require breaking down the KEY_FOO tokens into their constituent characters, and there is no preprocessor facility to do that. 至少,这将需要将KEY_FOO令牌分解为它们的组成字符,并且没有预处理器工具可以这样做。

It should be easy to do with sed or perl, or you could even write a C program to perform this transformation, but you can't do it in the preprocessor. 使用sed或perl应该很容易,否则您甚至可以编写C程序来执行此转换,但是您不能在预处理器中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM