C preprocessor problem

G

gpearman

Hi all,

I'm trying to generate a static string from two defines that are
passed in on the command line during make (via -D). I'll admit that my
understanding of the C preprocessor is not great ,and I can't work out
how to join the two together at compile time. At the moment I've tried

static const U8BIT *app_sw_ver_no_str =
(U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING

But this fails with gcc. Could anybody tell me how I can join the two
defines into a string?

Thanks,
Gaz.
 
J

jameskuyper

Hi all,

I'm trying to generate a static string from two defines that are
passed in on the command line during make (via -D). I'll admit that my
understanding of the C preprocessor is not great ,and I can't work out
how to join the two together at compile time. At the moment I've tried

static const U8BIT *app_sw_ver_no_str =
(U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING

But this fails with gcc. Could anybody tell me how I can join the two
defines into a string?

C automatically merges adjacent string literals separated only by
whitespace. Thus, "Hello, " "world!" is essentially the same as as
"Hello, world!". Therefore, it's actually much simpler than think.
All you have to do is:

static const U8BIT *app_sw_ver_no_str =
(U8BIT*) APP_VERSION_STRING APP_DATE_STRING;

Note: the # and ## operators are useful only within the replacement
list for the #define of a function-like macro.
 
R

Richard Heathfield

(e-mail address removed) said:
Hi all,

I'm trying to generate a static string from two defines that are
passed in on the command line during make (via -D). I'll admit that my
understanding of the C preprocessor is not great ,and I can't work out
how to join the two together at compile time. At the moment I've tried

static const U8BIT *app_sw_ver_no_str =
(U8BIT*)#APP_VERSION_STRING##APP_DATE_STRING

But this fails with gcc. Could anybody tell me how I can join the two
defines into a string?

http://c-faq.com/ansi/stringize.html

Note especially the sentence beginning "Note that both # and ## operate
only..."
 
O

Old Wolf

All you have to do is:

static const U8BIT *app_sw_ver_no_str =
(U8BIT*) APP_VERSION_STRING APP_DATE_STRING;

That assumes that the defines include quotes already,
e.g. -DAPP_VERSION_STRING="foo" . If not, then the OP
will have to use a stringize macro as well, e.g.

static const U8BIT *app_sw_ver_no_str = (U8BIT*)
STRINGIZE(APP_VERSION_STRING) STRINGIZE(APP_DATE_STRING);
 
J

jameskuyper

Old said:
That assumes that the defines include quotes already,

It figured that two macros both named with a _STRING suffix, intended
for use in C code, would have values that already had double quotes
around them. Still, I suppose it is possible.
 

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
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top