Re: Specifying "enum" data size
Re: Specifying "enum" data size
- Subject: Re: Specifying "enum" data size
- From: Andreas Grosam <email@hidden>
- Date: Sat, 24 Sep 2005 17:35:41 +0200
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.
Note, that for C++ this all is not a problem - as long as the compiler conforms to the ABI specification! The ABI specification guarantees binary compatibility, so you don't need to bother about the size of an enum type, much like the size of structs or classes.
Andreas
Chris _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden