473,388 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
Jan 10 '06 #1
6 14886
Bo Peng wrote:
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)

Jan 10 '06 #2
> #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
Jan 10 '06 #3
Bo Peng wrote:
#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.


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".
Jan 10 '06 #4
Bo Peng wrote:
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
Jan 10 '06 #5
Bo Peng wrote:
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.

--
Mike Smith
Jan 10 '06 #6
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
Jan 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Matthias | last post by:
Hello, I am missing certain functionality of std::string, so I am currently writing some helper functions which operate on strings. On of them is as follows (it's actually two functions): ...
4
by: aaronfude | last post by:
Hi, Perhaps this is slightly offtopic. Coming into the Windows world from Unix. Is there a reference on writing C++ libraries in a portable way. For example, right now I'm sticking...
3
by: SenderX | last post by:
I am porting a library from msvc++ to gcc and POSIX and was wondering if the following was even close to being portable/c-std: #define MEM_CAST unsigned char* #define MEM_TO_OBJ( m, o...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
6
by: pedroalves | last post by:
Hi all, This is not a question about how to #define COMMA , Please keep reading. Recently in binutils, we introduced a macro like this: #define STRING_COMMA_LEN(STR) \ (STR), ((STR) ?...
20
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
13
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.