J
Jonathan Avraham
Hi,
If you don't want to read the whole story:
I'm asking - Can I force gcc to treat all enums as 2-byte integers?
If you need more details - read on:
I'm trying to write an embedded program in C that communicates over ip and
uses a well defined protocol in gcc (currently I'm compiling for
WindRiver's VxWorks simulator, the system will eventually run on mips
architecture). In this protocol, some of the messages are defined as
structs which have fields that are enumerated. I need these fields to
take two bytes - this is what the protocol requires. However, by
default, gcc allocates 4 bytes per enum. Using -fshort-enums I can
tell it to automatically determine the required size, but this is
again a problem - most of the enums have less than 256 literals, which
means they will take only 1 byte instead of 2 when allocated.
Ofcourse, I can use somewhat ackward workarounds by defining another
literal such as LAST = 0xFFFF or just defining those structs with
16-bit integers instead of enums, but I would much prefer using a
compiler directive/command line option which will force enums to use 2
bytes, regardless of required size, if one exists. I know this kind of
option exists in other compilers I worked with in the past, such as
diab or even bcc (if I'm not mistaken) but I haven't been able to find
such an option in gcc.
Does anyone know of such option?
Thanks,
Jonathan Avraham.
If you don't want to read the whole story:
I'm asking - Can I force gcc to treat all enums as 2-byte integers?
If you need more details - read on:
I'm trying to write an embedded program in C that communicates over ip and
uses a well defined protocol in gcc (currently I'm compiling for
WindRiver's VxWorks simulator, the system will eventually run on mips
architecture). In this protocol, some of the messages are defined as
structs which have fields that are enumerated. I need these fields to
take two bytes - this is what the protocol requires. However, by
default, gcc allocates 4 bytes per enum. Using -fshort-enums I can
tell it to automatically determine the required size, but this is
again a problem - most of the enums have less than 256 literals, which
means they will take only 1 byte instead of 2 when allocated.
Ofcourse, I can use somewhat ackward workarounds by defining another
literal such as LAST = 0xFFFF or just defining those structs with
16-bit integers instead of enums, but I would much prefer using a
compiler directive/command line option which will force enums to use 2
bytes, regardless of required size, if one exists. I know this kind of
option exists in other compilers I worked with in the past, such as
diab or even bcc (if I'm not mistaken) but I haven't been able to find
such an option in gcc.
Does anyone know of such option?
Thanks,
Jonathan Avraham.