From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire) Newsgroups: comp.os.msdos.djgpp Subject: GCC/Info docs bug regarding __attribute__ Date: 1 Aug 1997 04:09:38 GMT Organization: The National Capital FreeNet Lines: 136 Message-ID: <5rrni2$48j@freenet-news.carleton.ca> Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire) NNTP-Posting-Host: freenet5.carleton.ca To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk In a program I am writing I am having problems using __attribute__ ((packed)) on a struct meant for reading the (unpacked) records of a binary file generated from other software (a Visual Basic app to be precise). As indicated in the documentation (info gcc) I have used this: struct etmp { ... } __attribute__ ((packed)); But, this generates the error: ../chem/element.cc(43) Error: semicolon missing after declaration of `etmp' According to the GCC dox this should work! I also tried ending with }; __attribute__ ((packed)); This causes: ../chem/element.cc(43) Warning: empty declaration which indicates that the __attribute__ is now being treated as a seperate item from the struct and is not working. Lastly I tried: struct etmp __attribute__ ((packed)) { ... }; But that causes: ../chem/element.cc(27) Error: parse error before `{' ../chem/element.cc(43) Error: parse error before `}' In method `element::element(void *)': ../chem/element.cc(84) Error: aggregate `struct etmp e' has incomplete type and cannot be initialized (27 is the struct line, 43 is the end of struct line, and 84 is the first line to try to refer to the struct.) Relevant clauses in info: Specifying Attributes of Types ============================== The keyword `__attribute__' allows you to specify special attributes of `struct' and `union' types when you define such types. This keyword is followed by an attribute specification inside double parentheses. Three attributes are currently defined for types: `aligned', `packed', and `transparent_union'. ... [therefore, __attribute__ ((packed)) is written correctly...] ... You may specify ... the `packed' attribute only past the closing brace of a [struct] definition. ... [therefore, struct foo { ... } __attribute__ ((packed)); is correct or struct foo { ... }; __attribute__ ((packed)); is correct, whereas the compiler barfs for both! This seems to be a contradiction.] ... For example, you could write: struct S { short f[3]; } __attribute__ ((aligned)); ... [This example implies the same syntax is correct for ((packed)), but that caused the missing semicolon error!] ...`packed' This attribute, attached to an `enum', `struct', or `union' type definition, specified that the minimum required memory be used to represent the type. Specifying this attribute for `struct' and `union' types is equivalent to specifying the `packed' attribute on each of the structure or union members. Specifying the `-fshort-enums' flag on the line is equivalent to specifying the `packed' attribute on all `enum' definitions. You may only specify this attribute after a closing curly brace on an `enum' definition, not in a `typedef' declaration. [Even if this also applies to 'struct', I do not have 'typedef struct etmp { ... } ETMP __attribute__ ((packed))', which would then be illegal, but merely 'struct etmp { ... } __attribute__ ((packed))'!] That is all the dox say about the packed attribute for types. I am forced to conclude one of three things is the case: 1. The syntax for the packed attribute differs from that for the aligned one, and the dox are incomplete in this area; and the __attribute__ ((packed)) belongs elsewhere in the struct. If so, I need urgently to know where, and the docs need updating. 2. The syntax is correct, but GCC has a bug. In that case I need to know of a workaround urgently, and GCC needs to be debugged and redistributed. 3. There are certain situations (Such as what? Structs with four letter names? Structs with doubles? Structs with strings? Mine has a double and a couple of fixed length strings, char foo[N]; style... C++ structs? Structs compiled during a full moon?!) in which attributes are not allowed, and the dox are incomplete and a workaround is again required urgently. In this case there is a buglet in GCC also, in the way of an obtuse error message that could be made more informative. Unfortunately, important work is at a virtual standstill until this bug gets resolved. (Maybe someone compiled a virus with a "class werewolf"... maybe it infected my compiler... Maybe my struct is undergoing lycanthropy, and turns into a class, with different syntax, during full moon nights!) ... C:\DJGPP> antivir -detect -clean -log -drives: ACD -memory -bootsectors ... -- .*. Where feelings are concerned, answers are rarely simple [GeneDeWeese] -() < When I go to the theater, I always go straight to the "bag and mix" `*' bulk candy section...because variety is the spice of life... [me] Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh