• Advertisement

Archived

This topic is now archived and is closed to further replies.

C Preprocessor HOWTO?

This topic is 5903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I change the way the following behaves #define DEF something #define AMACRO(a,b) a ## b #define SOMTHING AMACRO(DEF,"1"); now I know the above is pretty pointless but what I want to happen is SOMETHING==something1 but instead I get SOMETHING=DEF1 because the ##(concat) is happening before DEF gets resolved to something. What I am actually trying to do is the following: There is a define which I can not alter and it exists as follows: #define NAME somename now I want to include the file like so #include "somename_suffix" where somename is got from the define NAME. The problem is that in order to do #include NAME"_suffix" NAME has to be defined like so #define NAME "somename" Which for my own reasons is just NOT possible. I tried something like this #define addquotes(a) #a #include QNAME addquotes(NAME) #include QNAME"_suffix" but the results are as follows: #include "NAME""_suffix" which is the same as #include "NAME_suffix" which is WRONG! Is there a way I can tell the preprocessor to resolve it defines in a different order or is there a better way to do this? I really need help on this one. Cheers X2K

Share this post


Link to post
Share on other sites
Advertisement
After some experimention, I've found the following, which is close to the desired result.

    
#define NAME ...

#define STRING0(x) #x

#define STRING(x) STRING0(x)

#define CONCAT(x,y) STRING(x) ## y


My preprocessor doesn't appear to act sensibly: when one of the arguments to a ## is a substitution, it puts in whitespace around it. This appears to be against the standard (as well as the documentation for my preprocessor), though, so you may enjoy better luck if you're not using GNU cpp 2.95.2.

All your bases belong to us (I know. It's irony.)

Edit: SOURCE tag failed as usual.

Edited by - Mayrel on November 21, 2001 11:08:42 AM

Share this post


Link to post
Share on other sites

  • Advertisement