Eric Sosman
9/13/2011 11:55:00 AM
On 9/13/2011 5:47 AM, Wang WolfLouis wrote:
> Dear all,
>
> #define WIN32_PACKED #pragma pack(1)
> I want to define a Macro to simpilify and unify my code for Windows
> and Unix. The special character # is the key. anybody can give me some
> suggesstion? Thanks.
Can't be done this way. For one thing, the # in the macro's
definition is an operator, with a different meaning than it would
have elsewhere. For another, the Standard says (6.10.3.4p3)
The resulting completely macro-replaced preprocessing token
sequence is not processed as a preprocessing directive even
if it resembles one [...]
So a macro expansion cannot generate a preprocessing directive.
However, the same paragraph continues
[...] but all pragma unary operator expressions within
it are then processed as specified in 6.10.9 below.
The "C99" version of the Standard introduced a _Pragma operator to
help with exactly this situation. You could try
#define WIN32_PACKED _Pragma("pack(1)")
to get the effect you want. "Try," I said, because _Pragma was new
in C99, and I have heard that Microsoft's C implementations (which
it looks like you might be using) are mostly stuck in the C90 era,
before _Pragma came along. It's worth a try, though.
... if you really think you need "pack(1)". Usually (not always,
but usually) that's the sign of a short-term hack leading to long-
term headaches.
--
Eric Sosman
esosman@ieee-dot-org.invalid