Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 25th, 2006, 06:35 PM
Howard
Guest
 
Posts: n/a
Default Macros (I know, yuk!)

Hi,
I need to be able to define some code for one platform but not another,
where the code consists of preprocessor definitions. I'm trying to use a
macro (yuk), but having trouble (no surprise). The problem seems to be
trying to include preprocessor definitions inside the macro. These need to
be on their own line, but the macro expansion puts everything on the same
line. Is there any way to include preprocessor definitions in a macro? I
really don't like having to write out the code manually every place I need
it. Here's the code I need to add (in quite a few places):

#ifdef WIN32
#ifndef _DEBUG
#pragma code_seg(".ftext")
#endif
#endif

....some code functions go here, then...

#ifdef WIN32
#ifndef _DEBUG
#pragma code_seg()
#endif
#endif

It would be nice if I could just write:

CODESEG_ON()
....some code functions go here, then...
CODESEG_OFF()

And have that expand to the above for Windoze, but expand to nothing for
Mac.

Any ideas?

Thanks,
-Howard



  #2  
Old January 25th, 2006, 06:45 PM
Howard
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)


"Howard" <alicebt@hotmail.com> wrote in message
news:JiPBf.295179$qk4.49873@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Hi,
> I need to be able to define some code for one platform but not another,
> where the code consists of preprocessor definitions. I'm trying to use a
> macro (yuk), but having trouble (no surprise). The problem seems to be
> trying to include preprocessor definitions inside the macro. These need
> to be on their own line, but the macro expansion puts everything on the
> same line. Is there any way to include preprocessor definitions in a
> macro? I really don't like having to write out the code manually every
> place I need it. Here's the code I need to add (in quite a few places):
>
> #ifdef WIN32
> #ifndef _DEBUG
> #pragma code_seg(".ftext")
> #endif
> #endif
>
> ...some code functions go here, then...
>
> #ifdef WIN32
> #ifndef _DEBUG
> #pragma code_seg()
> #endif
> #endif
>
> It would be nice if I could just write:
>
> CODESEG_ON()
> ...some code functions go here, then...
> CODESEG_OFF()
>
> And have that expand to the above for Windoze, but expand to nothing for
> Mac.
>
> Any ideas?
>
> Thanks,
> -Howard[/color]

I tried re-ordering and re-defining my macros so I don't need multiple lines
(which worked, as far as that part goes), but I still get errors.
Apparently, I can't even _have_ a #-sign in the expanded macro! Is there
any way to accomplish this in C++?

-Howard


  #3  
Old January 25th, 2006, 06:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)

Howard wrote:[color=blue]
> I need to be able to define some code for one platform but not another,
> where the code consists of preprocessor definitions. I'm trying to use a
> macro (yuk), but having trouble (no surprise). The problem seems to be
> trying to include preprocessor definitions inside the macro. These need to
> be on their own line, but the macro expansion puts everything on the same
> line. Is there any way to include preprocessor definitions in a macro? I
> really don't like having to write out the code manually every place I need
> it. Here's the code I need to add (in quite a few places):
>
> #ifdef WIN32
> #ifndef _DEBUG
> #pragma code_seg(".ftext")
> #endif
> #endif
>
> ...some code functions go here, then...
>
> #ifdef WIN32
> #ifndef _DEBUG
> #pragma code_seg()
> #endif
> #endif
>
> It would be nice if I could just write:
>
> CODESEG_ON()
> ...some code functions go here, then...
> CODESEG_OFF()
>
> And have that expand to the above for Windoze, but expand to nothing for
> Mac.
>
> Any ideas?[/color]

You (or somebody else) already asked about this, like, a month ago. No,
there is no way. A preprocessor macro cannot expand into another
preprocessor directive. Or, rather, if it does, it won't get processed.

V
  #4  
Old January 25th, 2006, 07:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)

Howard wrote:[color=blue]
> "Howard" <alicebt@hotmail.com> wrote in message
> news:JiPBf.295179$qk4.49873@bgtnsc05-news.ops.worldnet.att.net...[color=green]
> > Hi,
> > I need to be able to define some code for one platform but not another,
> > where the code consists of preprocessor definitions. I'm trying to use a
> > macro (yuk), but having trouble (no surprise). The problem seems to be
> > trying to include preprocessor definitions inside the macro. These need
> > to be on their own line, but the macro expansion puts everything on the
> > same line. Is there any way to include preprocessor definitions in a
> > macro? I really don't like having to write out the code manually every
> > place I need it. Here's the code I need to add (in quite a few places):
> >
> > #ifdef WIN32
> > #ifndef _DEBUG
> > #pragma code_seg(".ftext")
> > #endif
> > #endif
> >
> > ...some code functions go here, then...
> >
> > #ifdef WIN32
> > #ifndef _DEBUG
> > #pragma code_seg()
> > #endif
> > #endif
> >
> > It would be nice if I could just write:
> >
> > CODESEG_ON()
> > ...some code functions go here, then...
> > CODESEG_OFF()
> >
> > And have that expand to the above for Windoze, but expand to nothing for
> > Mac.
> >
> > Any ideas?
> >
> > Thanks,
> > -Howard[/color]
>
> I tried re-ordering and re-defining my macros so I don't need multiple lines
> (which worked, as far as that part goes), but I still get errors.
> Apparently, I can't even _have_ a #-sign in the expanded macro! Is there
> any way to accomplish this in C++?
>
> -Howard[/color]

No, but you could make it a one-liner if you put that code in two .h
files. Then you could do this:

#include "CodeSegOn.h"
// ...
#include "CodeSegOff.h"

Cheers! --M

  #5  
Old January 25th, 2006, 07:05 PM
Howard
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)


"mlimber" <mlimber@gmail.com> wrote in message
news:1138214932.231648.68100@z14g2000cwz.googlegro ups.com...[color=blue]
> Howard wrote:[color=green]
>> "Howard" <alicebt@hotmail.com> wrote in message
>> news:JiPBf.295179$qk4.49873@bgtnsc05-news.ops.worldnet.att.net...[color=darkred]
>> > Hi,
>> > I need to be able to define some code for one platform but not
>> > another,
>> > where the code consists of preprocessor definitions. I'm trying to use
>> > a
>> > macro (yuk), but having trouble (no surprise). The problem seems to be
>> > trying to include preprocessor definitions inside the macro. These
>> > need
>> > to be on their own line, but the macro expansion puts everything on the
>> > same line. Is there any way to include preprocessor definitions in a
>> > macro? I really don't like having to write out the code manually every
>> > place I need it. Here's the code I need to add (in quite a few
>> > places):
>> >
>> > #ifdef WIN32
>> > #ifndef _DEBUG
>> > #pragma code_seg(".ftext")
>> > #endif
>> > #endif
>> >
>> > ...some code functions go here, then...
>> >
>> > #ifdef WIN32
>> > #ifndef _DEBUG
>> > #pragma code_seg()
>> > #endif
>> > #endif
>> >
>> > It would be nice if I could just write:
>> >
>> > CODESEG_ON()
>> > ...some code functions go here, then...
>> > CODESEG_OFF()
>> >
>> > And have that expand to the above for Windoze, but expand to nothing
>> > for
>> > Mac.
>> >
>> > Any ideas?
>> >
>> > Thanks,
>> > -Howard[/color]
>>
>> I tried re-ordering and re-defining my macros so I don't need multiple
>> lines
>> (which worked, as far as that part goes), but I still get errors.
>> Apparently, I can't even _have_ a #-sign in the expanded macro! Is there
>> any way to accomplish this in C++?
>>
>> -Howard[/color]
>
> No, but you could make it a one-liner if you put that code in two .h
> files. Then you could do this:
>
> #include "CodeSegOn.h"
> // ...
> #include "CodeSegOff.h"
>
> Cheers! --M
>[/color]

That would do the job, but I'm thinking I'll just stick with copy&pasting.
Less cryptic. (And, I can reduce it to three lines if I use "#if
defined(WIN32) && !defined(_DEBUG)".)

Thanks anyway, guys!
-Howard



  #6  
Old January 25th, 2006, 08:15 PM
Stephan Brönnimann
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)

Victor Bazarov wrote:[color=blue]
> Howard wrote:[color=green]
> > I need to be able to define some code for one platform but not another,
> > where the code consists of preprocessor definitions. I'm trying to use a
> > macro (yuk), but having trouble (no surprise). The problem seems to be
> > trying to include preprocessor definitions inside the macro. These need to
> > be on their own line, but the macro expansion puts everything on the same
> > line. Is there any way to include preprocessor definitions in a macro? I
> > really don't like having to write out the code manually every place I need
> > it. Here's the code I need to add (in quite a few places):
> >
> > #ifdef WIN32
> > #ifndef _DEBUG
> > #pragma code_seg(".ftext")
> > #endif
> > #endif
> >
> > ...some code functions go here, then...
> >
> > #ifdef WIN32
> > #ifndef _DEBUG
> > #pragma code_seg()
> > #endif
> > #endif
> >
> > It would be nice if I could just write:
> >
> > CODESEG_ON()
> > ...some code functions go here, then...
> > CODESEG_OFF()
> >
> > And have that expand to the above for Windoze, but expand to nothing for
> > Mac.
> >
> > Any ideas?[/color]
>
> You (or somebody else) already asked about this, like, a month ago. No,
> there is no way. A preprocessor macro cannot expand into another
> preprocessor directive.[/color]
[color=blue]
> Or, rather, if it does, it won't get processed.[/color]
Are you sure about this last statement? Are pragmas not evaluated
by the compiler? How could a preprocessor handle something like:

// Pragmas to disable cxx compiler warnings on Compaq Tru64 UNIX
#pragma message save
#pragma message disable (intconlosbit, setbutnotused, unrfunprm)
#include <legacy.h>
#pragma message restore

Regards, Stephan

  #7  
Old January 25th, 2006, 08:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)

Stephan Brönnimann wrote:[color=blue]
> Victor Bazarov wrote:[color=green]
>> [..] A preprocessor macro cannot expand into another
>>preprocessor directive.[/color][/color]
^^^^^^^^^^^^[color=blue]
>
>[color=green]
>>Or, rather, if it does, it won't get processed.[/color]
>
> Are you sure about this last statement? Are pragmas not evaluated
> by the compiler? How could a preprocessor handle something like:
>
> // Pragmas to disable cxx compiler warnings on Compaq Tru64 UNIX
> #pragma message save
> #pragma message disable (intconlosbit, setbutnotused, unrfunprm)
> #include <legacy.h>
> #pragma message restore[/color]

The preprocessor handles 'pragma' is an implementation-defined manner.
There is no standard behaviour when it comes to 'pragma's.

V
  #8  
Old January 25th, 2006, 11:05 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: Macros (I know, yuk!)

Howard wrote:[color=blue]
> Hi,
> I need to be able to define some code for one platform but not another,
> where the code consists of preprocessor definitions. I'm trying to use a
> macro (yuk), but having trouble (no surprise). The problem seems to be
> trying to include preprocessor definitions inside the macro. These need to
> be on their own line, but the macro expansion puts everything on the same
> line. Is there any way to include preprocessor definitions in a macro? I
> really don't like having to write out the code manually every place I need
> it. Here's the code I need to add (in quite a few places):[/color]

[ ... ]
[color=blue]
> It would be nice if I could just write:
>
> CODESEG_ON()[/color]

How about:

#include "codeseg_on"

and:

#include "codeseg_off"

?

--
Later,
Jerry.

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles