• Advertisement
Sign in to follow this  

#define MACRO question

This topic is 4676 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

Below are some snippets of some code I'm working on. I'm working on setting up a little platform independant architecture library. I'm having trouble with a #define constant being used within a #define macro.
//  This value will depend on the setup configuration.
//  While this could potentially be a const instead of define, right
//  now I want it purely in the preprocessor
#define	ARCHITECTURE_STRING	"X86_32"

//  The multibyte macro - converts strings to multibyte
//  There is also a version of TEXT(x) that is just x for non-multibyte
#define	TEXT(x)	L##x

//  Basically converts the string to the appripriate characters set
//  Be it multibyte or single byte, depending on how TEXT is defined
#define	ARCHITECTURE_NAME	TEXT( ARCHITECTURE_STRING )


What I get when I try to print is LARCHITECTURE_STRING being passed in place of ARCHTECITURE_NAME, when I'm hoping for the multibyte translation of ARCHITECTURE_STRING L"X86_32". Is there a way to get this to work, while leaving these all using #defines? I would also like to note, I am using Visual C++ 2005 Express beta.

Share this post


Link to post
Share on other sites
Advertisement
You'll have to use an extra level of macro indirection to resolve the parameter's value.

Share this post


Link to post
Share on other sites
I've never been able to get a define with in a define to work. The #define see's teh '#' from the other #define and doesn't know what to do with it.

Hopefully some one here can show me how. I wanted to do it for vs.net #pragma's and gcc #warnings so I could create a cross platform compile time messaging library.

Good luck, let me know if you get a solution.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
What do you mean?
I mean like this:
//  This value will depend on the setup configuration.
// While this could potentially be a const instead of define, right
// now I want it purely in the preprocessor
#define ARCHITECTURE_STRING "X86_32"

// The multibyte macro - converts strings to multibyte
// There is also a version of TEXT(x) that is just x for non-multibyte
#define TEXT(x) L##x
#define INDIRECT_TEXT(x) TEXT( x )

// Basically converts the string to the appripriate characters set
// Be it multibyte or single byte, depending on how TEXT is defined
#define ARCHITECTURE_NAME INDIRECT_TEXT( ARCHITECTURE_STRING )

Share this post


Link to post
Share on other sites
boost has some macros defined that will do what you are looking for. IIRC you want to look at BOOST_PP_CAT

Share this post


Link to post
Share on other sites
I don't believe BOOST_PP_CAT will work for definitions that have '#' in them

CHeers
CHris

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
doynax,
Have you tested your solution?? I"m not so sure that will work:)


His method works fine and is the standard solution to such problems...

Share this post


Link to post
Share on other sites
Really, well that's great!!!

Can you show me how to get this to work with BOOST_PP_CAT.

#ifdef _WIN32
#define message(x) #pragma message "hello"
#else
#define message(x) #warningmessage "hello"
#endif

I also tried putting '#' in my definitions and the BOOST_PP_CAT causes preprocessor to error, Since you say its possible, how do you get '#' in preprocessor macros?? I'd love this feature:)

Bye the way you usually seem like a nice guy/girl, what's up with the terse answer:)

Cheers
CHris

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
Really, well that's great!!!

Can you show me how to get this to work with BOOST_PP_CAT.

#ifdef _WIN32
#define message(x) #pragma message "hello"
#else
#define message(x) #warningmessage "hello"
#endif

I also tried putting '#' in my definitions and the BOOST_PP_CAT causes preprocessor to error, Since you say its possible, how do you get '#' in preprocessor macros?? I'd love this feature:)

Bye the way you usually seem like a nice guy/girl, what's up with the terse answer:)

Cheers
CHris


As far as I know, there isn't a real good solution for this. The closest thing I have done to this is something along the lines of

#ifdef ENABLE_DEBUG_FILE_MESSAGES
#define DEBUG_FILE_PRAGMA message( "Parsing " __FILE__ )
#else
#define DEBUG_FILE_PRAGMA
#endif

and then put the following line at the top of my files after the includes

#pragma DEBUG_FILE_PRAGMA

Then I can toggle on the ability to display all header the files that are being parsed when compiling a cpp file.

Share this post


Link to post
Share on other sites
Why not do this instead:


const char* ARCHITECTURE_STRING = "X86_32";

#define TEXT(x) L##x

const wchar_t* ARCHITECTURE_NAME = TEXT(ARCHITECTURE_STRING);



Try to avoid using macros if there's another thing that can replace them (in this case I'm using constant variables).

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
I don't believe BOOST_PP_CAT will work for definitions that have '#' in them

CHeers
CHris

I was responding to the OP's example, sorry for any confusion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement