On 24.09.2005, at 02:17, Chris Espinosa wrote:
On Sep 23, 2005, at 5:04 PM, Mark Wagner wrote:
How do I specify the size of an "enum" value? I've got a program that
links to a number of static libraries. In one of them, an "enum" for
the typedef'd "BoolEnum" type is one bit, while in another, it's 32
bits. Needless to say, this causes problems when passing structs
containing BoolEnums back and forth.
Does GCC recognize the "#pragma enumsalwaysint" pragma? Might the
presence of this in some of the source files be causing problems?
I'm using XCode 1.5 and GCC 3.3, and I'm writing in C.
The only control available is -fshort-enums ("Short Enumeration
Constants") which you can apply on a target or on a single file.
There's no pragma to make this apply only to specific enums within a
file. This will make enums as small as possible for the range of
enums given.
Notice that Mac OS X frameworks often declare structs that contain
enumerated values, so changing the size of enums might make your code
unable to call Mac OS X system functions.
Are you sure? I thought, enums were only be used to declare constants
and never be used as types. Thus, enums do not have any effect on the
layout or sizeof of structs.
Otherwise, in the C language this would be a very fragile usage. In C,
i would never ever use enums as types in interfaces since the size of
the enum type is compiler "implementation defined", may depend on
optimization flags, architecture, etc.
Note, that binary compatibility (here the size of enums) may only be
guaranteed when you use the same C compiler.
I also wouldn't use the switch for enums used in interfaces. The
documentation says this:
"Warning: the -fshort-enums switch causes GCC to generate code that is
not binary compatible with code generated without that switch. Use it
to conform to a non-default application binary interface. "
In your case, it looks like, that your static library has been compiled
with this switch on, or with another compiler, or other compiler flags
changing the size of the enum - thus it is not binary compatible
anymore.
Well, to workaround your problem, re-compile the static libraries using
*your* compiler (gcc 3.3) in order to get ABI compatibility.