fread2281
fread2281

Reputation: 1136

C meta macro pattern matching

I have some macros KEY_*. I want to define them all as extern variables, but the number and name may vary.

file1.h:

#define KEY_FOO 200
...
#define KEY_ASDFG 423

file2.c after preprocessor:

#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?

Upvotes: 0

Views: 1244

Answers (1)

Samuel Edwin Ward
Samuel Edwin Ward

Reputation: 6675

You can't do this using only the C preprocessor.

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.

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.

Upvotes: 1

Related Questions