A portable way to pass a string to C++ as macro?

B

Bo Peng

Dear list,

I am having trouble with a seemingly very simple problem, namely, I need
to pass a string to C++ code through macro definition:

gcc -DVERSION=0.6.7 test.cpp

while in test.cpp

string version()
{
return VERSION;
}

The above of course does not work. I have tried:

1. gcc -DVERSION="0.6.7" test.cpp
or gcc -DVERSION=\"0.6.7\" test.cpp
works under gcc/linux but not windows/mingw32 gcc. The " somehow get
lost.

2. gcc -DVERSION=0.6.7 test.cpp
but in tet.cpp use a token-paste operator ## to convert 0.6.9 to "0.6.9"

string version()
{
#define VER " ## VERSION ## "
return VER
}

This works fine under linux, windows, but failed on a cluster maehine
(ia64) with gcc 3.2.3. It treat VER as ## VERSION ## .

So, maybe I am missing something really obvious here, what is a portable
way to do this?

Many thanks in advance.
Bo
 
R

red floyd

Bo said:
Dear list,

I am having trouble with a seemingly very simple problem, namely, I need
to pass a string to C++ code through macro definition:

gcc -DVERSION=0.6.7 test.cpp

while in test.cpp

string version()
{
return VERSION;
}

The above of course does not work. I have tried:

1. gcc -DVERSION="0.6.7" test.cpp
or gcc -DVERSION=\"0.6.7\" test.cpp
works under gcc/linux but not windows/mingw32 gcc. The " somehow get
lost.

2. gcc -DVERSION=0.6.7 test.cpp
but in tet.cpp use a token-paste operator ## to convert 0.6.9 to "0.6.9"

string version()
{
#define VER " ## VERSION ## "
return VER
}

This works fine under linux, windows, but failed on a cluster maehine
(ia64) with gcc 3.2.3. It treat VER as ## VERSION ## .

So, maybe I am missing something really obvious here, what is a portable
way to do this?

Many thanks in advance.
Bo

#define VER1_(x) #x
#define VER_(x) VER1_(x)
#define VER VER_(VERSION)
 
B

Bo Peng

#define VER1_(x) #x
#define VER_(x) VER1_(x)
#define VER VER_(VERSION)

I admit that I was waiting for something simpler than three '#define's.
Anyway, this works, hopefully for all gcc versions.

Thank you very much for your quick response.
Bo
 
R

red floyd

Bo said:
I admit that I was waiting for something simpler than three '#define's.
Anyway, this works, hopefully for all gcc versions.

Alas, you need the 3 defines because if you just have

#define VER_(x) #x
#define VER VER_(VERSION)

Then VER winds up being the literal string "VERSION". You need the
extra macro to cause VERSION to be interpreted before it gets "stringized".
 
R

Rennie deGraaf

Bo said:
Dear list,

I am having trouble with a seemingly very simple problem, namely, I need
to pass a string to C++ code through macro definition:

gcc -DVERSION=0.6.7 test.cpp

while in test.cpp

string version()
{
return VERSION;
}

Add the following somewhere in your file:

#define STRINGIFY(x) XSTRINGIFY(x)
#define XSTRINGIFY(x) #x

Then redefine your function like this:

string version()
{
return STRINGIFY(VERSION);
}

The macro "XSTRINGIFY" is needed in this case because of how macros are
expanded; don't call it directly.

Rennie deGraaf
 
M

Mike Smith

Bo said:
Dear list,

I am having trouble with a seemingly very simple problem, namely, I need
to pass a string to C++ code through macro definition:

gcc -DVERSION=0.6.7 test.cpp

while in test.cpp

string version()
{
return VERSION;
}

Silly question, but did you try:

// I'm assuming that using std::string appears somewhere in your code?
string version()
{
return string(VERSION);
}

....? VERSION, once expanded, will be a literal of type char const *,
but version() returns a string. Also, it might be helpful if you were
to tell us exactly what you expect version() to return.
 
B

Bo Peng

Silly question, but did you try:

// I'm assuming that using std::string appears somewhere in your code?
string version()
{
return string(VERSION);
}

No. This will not work. string(VERSION) will be expanded to
string(0.6.9) and gcc has no idea what 0.6.9 is. This is exactly why I
needed "0.6.9", instead of 0.6.9.

Bo
 

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

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top