Manipulating string constants with preprocessor macros - possible?

V

Veit Wiessner

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
 
R

ram_nrk200

Veit said:
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?

#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.
 
N

nrk

ram_nrk200 said:
#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.

Apologies for the crap 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.
 
V

Veit Wiessner

nrk said:
Apologies for the crap 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.

Exactly what I wanted, thank you very much!

Veit
 
J

John Navil Joseph

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
 
V

Veit Wiessner

John said:
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?

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
 

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

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top