Manipulating string constants with preprocessor macros - possible?

Manipulating string constants with preprocessor macros - possible?

Post by Veit Wiess » Thu, 04 Dec 2003 12:53:18


I wrote a program that handles the buildcount of projects (gets called
every time I compile a project, it writes a header file which is
#include-ed in the project).
My question is this, is it possible to modify string constants with
preprocessor macros? for now I write every string I need as a #define
in the generated header, when in theory I only need to define the
buildcount there and put it then in the places where I need it.

for instance:

#define BUILD 1


somewhere in the program I have string constant

char *version = "XYZ Version 2.3.21 Build X";

now I want to replace the X with the actual buildcount, but macros
inside quotationmarks don't work. Is there a way to somehow put the
quotationmarks around the string constant after the BUILD macro was
applied? With a macro?

I hope I made my problem clear, TIA :-)

Veit

--
if life throws you lemons make lemonade
 
 
 

Manipulating string constants with preprocessor macros - possible?

Post by ram_nrk20 » Thu, 04 Dec 2003 14:00:23


#define STRINGIFY(x) #x
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " STRINGIFY(BUILD);

int main(void) {
printf("Build is %s\n", version);
return 0;
}

HTH,
-nrk.

 
 
 

Manipulating string constants with preprocessor macros - possible?

Post by nrk » Thu, 04 Dec 2003 14:15:08


Apologies for the *** above, should've run the darn thing before posting.
Here, try this one... it should work:

#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);

int main(void) {
printf("Build is %s\n", version);
return 0;
}

-nrk.
 
 
 

Manipulating string constants with preprocessor macros - possible?

Post by Veit Wiess » Thu, 04 Dec 2003 15:12:59


Exactly what I wanted, thank you very much!

Veit

--
if life throws you lemons make lemonade
 
 
 

Manipulating string constants with preprocessor macros - possible?

Post by John Navil » Thu, 04 Dec 2003 22:15:37

I am interested in knowing how you are incrementing the build count.
Are you doing it manually or by making use of a 'makefile' that does
this? and if so how?

TIA,
john
 
 
 

Manipulating string constants with preprocessor macros - possible?

Post by Veit Wiess » Thu, 04 Dec 2003 23:37:53


I do it in the makefile, I added a 'buildcount' target (which only
calls the buildcount program itself) and the obj-targets which include
the generated header depend on it.


Veit

--
if life throws you lemons make lemonade