1

I want to concatenate two wide strings using a macro, so I define some macros:

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_BUILD 0
#define VERSION_REVISION 0


#define _STR(s) #s
#define STR(s) _STR(s)

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)


//http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
#define PRODUCT_ELASTOS_VERSION STR(VERSION_MAJOR) "." \
                                STR(VERSION_MINOR) "." \
                                STR(VERSION_BUILD) "." \
                                STR(VERSION_REVISION)

now I want to define a new macro PRODUCT_ELASTOS_VERSION_W using macro PRODUCT_ELASTOS_VERSION, it's value should be L"1.1.0.0". so how can I define this macro? TOWSTRING(PRODUCT_ELASTOS_VERSION) is wrong answer.

And if I want to concatenating string, how should I write? L"v" TOWSTRING(PRODUCT_ELASTOS_VERSION) cann't get wide string L"v1.1.0.0".

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
xufan
  • 392
  • 1
  • 5
  • 18
  • Note that `_` followed by UPPER-CASE letter at the beginning of an identifier is a reserved word (could be c++ only though, I'm not sure). You can put the `_` at the end instead, like `STR_` and `TOWSTRING_`. – Shahbaz Mar 01 '12 at 23:43
  • Also, shouldn't REVISION come after BUILD? And about keeping build counts, take a look [here](http://stackoverflow.com/questions/7713459/how-do-you-track-the-build-count-of-your-library-when-there-are-multiple-authors) maybe it could be useful (or you may have a better idea!) – Shahbaz Mar 01 '12 at 23:44

1 Answers1

0

First, PRODUCT_ELASTOS_VERSION does not expand to "1.1.0.0", it expands to

"1" "." "1" "." "0" "." "0"

Keeping the same structure you can define another identifier that expands to

L"1" L"." L"1" L"." L"0" L"." L"0"

with

#define _LSTR(s) L ## #s
#define LSTR(s) _LSTR(s)

#define ANOTHER_IDENTIFIER LSTR(VERSION_MAJOR) L"." \
                           LSTR(VERSION_MINOR) L"." \
                           LSTR(VERSION_BUILD) L"." \
                           LSTR(VERSION_REVISION)
pmg
  • 106,608
  • 13
  • 126
  • 198
  • It's not the best way, because if I define PRODUCT_NAME "hello wold", then wprintf(L"%s\n", L"v" TOWSTRING(PRODUCT_NAME)) works ok, but wprintf(L"%s\n", TOWSTRING(PRODUCT_VERSION)) is wrong.I want that each macro can works in the same way. – xufan Oct 01 '10 at 03:51