Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C preprocessor problem

Reply
Thread Tools

C preprocessor problem

 
 
gpearman@gmail.com
Guest
Posts: n/a
 
      11-12-2007
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.

 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      11-12-2007

(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-12-2007
http://www.velocityreviews.com/forums/(E-Mail 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..."

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-12-2007
On Nov 13, 11:49 am, (E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > 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).

>
> 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);


 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      11-12-2007

Old Wolf wrote:
> On Nov 13, 11:49 am, (E-Mail Removed) wrote:
> > (E-Mail Removed) wrote:
> > > 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).

> >
> > 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,


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
token pasting problem in K&R preprocessor Henry Townsend C Programming 13 06-24-2006 02:51 AM
preprocessor problem onkar C Programming 3 06-06-2006 09:06 AM
Preprocessor problem with Visual Age 6 Johnny Willemsen C++ 0 05-02-2005 07:31 PM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM



Advertisments