how do I let #define defines a macro

B

badc0de

Hello..

a header file of one of my project has macro definitions like this
(for example)

#define PART_A_SORT_1_LABEL_STRING1 100
#define PART_A_SORT_1_LABEL_STRING2 200
#define PART_A_SORT_1_LABEL_STRING3 300
#define PART_A_SORT_1_LABEL_STRING4 400
#define PART_A_SORT_1_LABEL_STRING5 500
....

#define PART_A_SORT_7_LABEL_STRING1 1100
#define PART_A_SORT_7_LABEL_STRING2 3200
#define PART_A_SORT_7_LABEL_STRING3 2300
#define PART_A_SORT_7_LABEL_STRING4 1400
#define PART_A_SORT_7_LABEL_STRING5 6500

so, I tried to make a Macro defines another macros like this.

#define MY_DEFINE(PART_SORT, C1, C2, C3, C4, C5) \
#define PART_SORT##_LABEL_STRING1 C1 \
#define PART_SORT##_LABEL_STRING2 C2 \
#define PART_SORT##_LABEL_STRING3 C3 \
#define PART_SORT##_LABEL_STRING4 C4 \
#define PART_SORT##_LABEL_STRING5 C5

yeah, I thought the first ugly, big bulk of definitions will be
compacted like this.

MY_DEFINE(PART_A_SORT_1, 100, 200, 300, 400, 500)
....
MY_DEFINE(PART_A_SORT_7, 1100, 3200, 2300, 1400, 6500)


but I found #define treats '#' and '##' special. ## concats, # makes
string.

Now I guess you know what I'm trying to do.

Is there WAY to achieve this? or
REASON why I cannot?

Reguards.
 
G

Gordon Burditt

#define MY_DEFINE(PART_SORT, C1, C2, C3, C4, C5) \
#define PART_SORT##_LABEL_STRING1 C1 \
#define PART_SORT##_LABEL_STRING2 C2 \
#define PART_SORT##_LABEL_STRING3 C3 \
#define PART_SORT##_LABEL_STRING4 C4 \
#define PART_SORT##_LABEL_STRING5 C5

yeah, I thought the first ugly, big bulk of definitions will be
compacted like this.

MY_DEFINE(PART_A_SORT_1, 100, 200, 300, 400, 500)
...
MY_DEFINE(PART_A_SORT_7, 1100, 3200, 2300, 1400, 6500)


but I found #define treats '#' and '##' special. ## concats, # makes
string.

The expansion of a macro may not generate a preprocessor directive
even if the expansion looks like one.
Now I guess you know what I'm trying to do.

Is there WAY to achieve this? or

No - not in normal C.
REASON why I cannot?

Once you generate the preprocessor directive using the preprocessor,
it's too late to interpret it as a preprocessor directive. Unless
you manage to run things through the preprocessor TWICE. This is
likely to cause other problems and there's no guarantee that the
preprocessor is a separate program that can accept its own output
as input. In actual practice you can probably get away with it.

If you want to pre-process C files before compiling them, there are
better macro languages than the C preprocessor. m4 might be better
for what you want. This, of course, requires some adjustment in
your build procedure. Some programs used to process input to produce
C code (such as the Oracle Pro*C compiler, yacc, and early versions
of C++) can do some really exotic things to the source code.
 
B

badc0de

The expansion of a macro may not generate a preprocessor directive
even if the expansion looks like one.



No - not in normal C.


Once you generate the preprocessor directive using the preprocessor,
it's too late to interpret it as a preprocessor directive. Unless
you manage to run things through the preprocessor TWICE. This is
likely to cause other problems and there's no guarantee that the
preprocessor is a separate program that can accept its own output
as input. In actual practice you can probably get away with it.

If you want to pre-process C files before compiling them, there are
better macro languages than the C preprocessor. m4 might be better
for what you want. This, of course, requires some adjustment in
your build procedure. Some programs used to process input to produce
C code (such as the Oracle Pro*C compiler, yacc, and early versions
of C++) can do some really exotic things to the source code.- -

- -

Yes, that's it. So it was..
I can clearly understand now.
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top