Macros (I know, yuk!)

H

Howard

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
 
V

Victor Bazarov

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

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
 
H

Howard

Howard said:
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

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
 
M

mlimber

Howard said:
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

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
 
H

Howard

mlimber said:
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

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
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Victor said:
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.
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
 
V

Victor Bazarov

Stephan said:
Victor said:
[..] A preprocessor macro cannot expand into another
preprocessor directive.
^^^^^^^^^^^^

Or, rather, if it does, it won't get processed.

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

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

V
 
J

Jerry Coffin

Howard said:
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):

[ ... ]
It would be nice if I could just write:

CODESEG_ON()

How about:

#include "codeseg_on"

and:

#include "codeseg_off"

?
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top