Mail Archives: djgpp/1996/12/07/13:12:08
Erik Max Francis wrote:
>
> John M. Aldrich wrote:
>
> > #pragma is an evil, hackish way to make gcc pack structures. The DJGPP
> > Frequently Asked Questions list (v2/faq202b.zip), chapter 22.9, tells
> > you exactly how to go about packing structs in DJGPP, and refers you to
> > the gcc reference which details the syntax of the __attribute__()
> > extension.
>
> I'm curious -- what's "unevil" or "unhackish" about using attributes to
> pack structures? Both are completely unportable, but it seems to me that
> the #pragma will work in the widest number of cases (i.e., taking into
> account old gcc bugs).
__attribute__ is supported on gcc 2.5 or later versions, and this can
easily be tested for in your programs with a block like the following:
#if defined(__GNUC__) && __GNUC__ >= 2 && __GNUC_MINOR__ >= 5
...
#endif
Also, you can use a similar test to insert a #pragma pack if you are
writing C++ and the version is less than 2.7.2.1. Or you can create the
necessary packed structs using extern "C" {...}
There is, as far as I know, no such easy way to test whether #pragma
pack will work on any given compiler. I have read many statements by
experienced programmers, all of whom say that #pragma is a very bad
thing to rely upon in a portable program.
Finally, nearly every compiler that aligns structs uses a different
method to specify that they should be packed. If you depend on specific
structure alignment to make your programs work, then you have no
alternative but to find out how to pack structs on each possible 32-bit
compiler, and write in portability code. This is unfortunate, but IMHO
it's better to rely on a concrete, defined instance of a packing
operation than a nebulous #pragma that may or may not be supported.
--
John M. Aldrich, aka Fighteer I <fighteer AT cs DOT com>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s+:- a-->? c++>$ U@>++$ p>+ L>++ E>+ W++ N++ o+ K? w(---) O-
M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+ tv+() b+++ DI++ D++ G e(*)>++++
h!() !r !y+()
------END GEEK CODE BLOCK------
- Raw text -