* weird structure definition in header file
@ 2005-09-06 19:13 Robert P. J. Day
2005-09-06 19:23 ` nhorman
2005-09-06 20:24 ` Ronaldo.Afonso
0 siblings, 2 replies; 9+ messages in thread
From: Robert P. J. Day @ 2005-09-06 19:13 UTC (permalink / raw)
To: C programming list
i'm looking at some legacy code and, in a header file, i find the
following (paraphrased for brevity):
typedef struct {
... stuff ...
} Widgets ;
extern Widgets Widget ;
huh? i can see why a header file would want to define a structure
but i'm confused why the *header* file would then refer to an external
object of that type. that's a new one on me -- typically, i'd expect
a *source* file to define such a thing and other *source* files to
contain the "extern" declaration.
is this some subtle programming cleverness of which i am unaware?
thanks.
rday
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: weird structure definition in header file 2005-09-06 19:13 weird structure definition in header file Robert P. J. Day @ 2005-09-06 19:23 ` nhorman 2005-09-06 19:27 ` Robert P. J. Day 2005-09-06 19:57 ` Steve Graegert 2005-09-06 20:24 ` Ronaldo.Afonso 1 sibling, 2 replies; 9+ messages in thread From: nhorman @ 2005-09-06 19:23 UTC (permalink / raw) To: Robert P. J. Day; +Cc: C programming list On Tue, Sep 06, 2005 at 03:13:48PM -0400, Robert P. J. Day wrote: > > i'm looking at some legacy code and, in a header file, i find the > following (paraphrased for brevity): > > typedef struct { > ... stuff ... > } Widgets ; > > extern Widgets Widget ; > > > huh? i can see why a header file would want to define a structure > but i'm confused why the *header* file would then refer to an external > object of that type. that's a new one on me -- typically, i'd expect > a *source* file to define such a thing and other *source* files to > contain the "extern" declaration. > > is this some subtle programming cleverness of which i am unaware? > thanks. > This is done quite frequently when a data structure needs to be referenced from multiple locations. They're not necessicarily consecutive like that, but its rather a common practice to extern a instance of a type in a header file. The kernel source tree does this very frequently, check include/net/ipv4 for lots of examples. Regards Neil > rday > - > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html -- /*************************************************** *Neil Horman *Software Engineer *gpg keyid: 1024D / 0x92A74FA1 - http://pgp.mit.edu ***************************************************/ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:23 ` nhorman @ 2005-09-06 19:27 ` Robert P. J. Day 2005-09-06 19:45 ` Robert P. J. Day 2005-09-07 1:17 ` Glynn Clements 2005-09-06 19:57 ` Steve Graegert 1 sibling, 2 replies; 9+ messages in thread From: Robert P. J. Day @ 2005-09-06 19:27 UTC (permalink / raw) To: nhorman; +Cc: C programming list On Tue, 6 Sep 2005, nhorman@tuxdriver.com wrote: > On Tue, Sep 06, 2005 at 03:13:48PM -0400, Robert P. J. Day wrote: > > > > i'm looking at some legacy code and, in a header file, i find the > > following (paraphrased for brevity): > > > > typedef struct { > > ... stuff ... > > } Widgets ; > > > > extern Widgets Widget ; > > > > > > huh? i can see why a header file would want to define a structure > > but i'm confused why the *header* file would then refer to an external > > object of that type. that's a new one on me -- typically, i'd expect > > a *source* file to define such a thing and other *source* files to > > contain the "extern" declaration. > > > > is this some subtle programming cleverness of which i am unaware? > > thanks. > > > This is done quite frequently when a data structure needs to be > referenced from multiple locations. They're not necessicarily > consecutive like that, but its rather a common practice to extern a > instance of a type in a header file. The kernel source tree does > this very frequently, check include/net/ipv4 for lots of examples. ok, so what it represents is just a shortcut so that each source file doesn't have to have its own extern, it just comes along with the header file. all right, i guess i can live with that although it strikes me as just a little bit sleazy. oh, well. rday ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:27 ` Robert P. J. Day @ 2005-09-06 19:45 ` Robert P. J. Day 2005-09-06 19:56 ` nhorman 2005-09-07 1:20 ` Glynn Clements 2005-09-07 1:17 ` Glynn Clements 1 sibling, 2 replies; 9+ messages in thread From: Robert P. J. Day @ 2005-09-06 19:45 UTC (permalink / raw) To: nhorman; +Cc: C programming list On Tue, 6 Sep 2005, Robert P. J. Day wrote: > On Tue, 6 Sep 2005, nhorman@tuxdriver.com wrote: > > > On Tue, Sep 06, 2005 at 03:13:48PM -0400, Robert P. J. Day wrote: > > > > > > i'm looking at some legacy code and, in a header file, i find the > > > following (paraphrased for brevity): > > > > > > typedef struct { > > > ... stuff ... > > > } Widgets ; > > > > > > extern Widgets Widget ; > > > > > > > > > huh? i can see why a header file would want to define a structure > > > but i'm confused why the *header* file would then refer to an external > > > object of that type. that's a new one on me -- typically, i'd expect > > > a *source* file to define such a thing and other *source* files to > > > contain the "extern" declaration. > > > > > > is this some subtle programming cleverness of which i am unaware? > > > thanks. > > > > > This is done quite frequently when a data structure needs to be > > referenced from multiple locations. They're not necessicarily > > consecutive like that, but its rather a common practice to extern a > > instance of a type in a header file. The kernel source tree does > > this very frequently, check include/net/ipv4 for lots of examples. um, still a couple questions on this. first, in terms of examples, there *is* no include/net/ipv4 directory in the kernel source tree anymore, it's just net/ipv4. and there's only a single header file in there, which has no example of such a practise. were you thinking of a different directory? (i'm in the 2.6.12.5 source tree.) also, i'm assuming that some source file must eventually include an actual definition of a "Widget" for the purposes of linking, as in: #include "widgets.h" ... Widgets Widget ; however, given the inclusion of the header file, doesn't this give me both a referencing declaration and a defining declaration of that object in the same file? is there no problem with that? i was under the impression that common practise was to have a single defining declaration and all the *remaining* be referencing declarations. rday ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:45 ` Robert P. J. Day @ 2005-09-06 19:56 ` nhorman 2005-09-07 1:20 ` Glynn Clements 1 sibling, 0 replies; 9+ messages in thread From: nhorman @ 2005-09-06 19:56 UTC (permalink / raw) To: Robert P. J. Day; +Cc: C programming list On Tue, Sep 06, 2005 at 03:45:01PM -0400, Robert P. J. Day wrote: > On Tue, 6 Sep 2005, Robert P. J. Day wrote: > > > On Tue, 6 Sep 2005, nhorman@tuxdriver.com wrote: > > > > > On Tue, Sep 06, 2005 at 03:13:48PM -0400, Robert P. J. Day wrote: > > > > > > > > i'm looking at some legacy code and, in a header file, i find the > > > > following (paraphrased for brevity): > > > > > > > > typedef struct { > > > > ... stuff ... > > > > } Widgets ; > > > > > > > > extern Widgets Widget ; > > > > > > > > > > > > huh? i can see why a header file would want to define a structure > > > > but i'm confused why the *header* file would then refer to an external > > > > object of that type. that's a new one on me -- typically, i'd expect > > > > a *source* file to define such a thing and other *source* files to > > > > contain the "extern" declaration. > > > > > > > > is this some subtle programming cleverness of which i am unaware? > > > > thanks. > > > > > > > This is done quite frequently when a data structure needs to be > > > referenced from multiple locations. They're not necessicarily > > > consecutive like that, but its rather a common practice to extern a > > > instance of a type in a header file. The kernel source tree does > > > this very frequently, check include/net/ipv4 for lots of examples. > > um, still a couple questions on this. first, in terms of examples, > there *is* no include/net/ipv4 directory in the kernel source tree > anymore, it's just net/ipv4. and there's only a single header file in > there, which has no example of such a practise. were you thinking of > a different directory? (i'm in the 2.6.12.5 source tree.) > I think you have a bad kernel tree: http://www.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=tree There is pretty clearly an include subdirectory there. > also, i'm assuming that some source file must eventually include an > actual definition of a "Widget" for the purposes of linking, as in: > > #include "widgets.h" > ... > Widgets Widget ; > > however, given the inclusion of the header file, doesn't this give me > both a referencing declaration and a defining declaration of that > object in the same file? is there no problem with that? i was under > the impression that common practise was to have a single defining > declaration and all the *remaining* be referencing declarations. > No. You've got a defining declaration (the typedef struct {...} widgets;) and a declaration which specifically defines an instance of the type widgets (named widget) as being instatiated externally to the scope of this file. Theres nothing wrong with doing that. Its exactly the same as placing an extern directive in each C file that includes the header. Its not at all problematic (or sleazy :) ). Neil > rday > > - > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html -- /*************************************************** *Neil Horman *Software Engineer *gpg keyid: 1024D / 0x92A74FA1 - http://pgp.mit.edu ***************************************************/ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:45 ` Robert P. J. Day 2005-09-06 19:56 ` nhorman @ 2005-09-07 1:20 ` Glynn Clements 1 sibling, 0 replies; 9+ messages in thread From: Glynn Clements @ 2005-09-07 1:20 UTC (permalink / raw) To: Robert P. J. Day; +Cc: nhorman, C programming list Robert P. J. Day wrote: > also, i'm assuming that some source file must eventually include an > actual definition of a "Widget" for the purposes of linking, as in: > > #include "widgets.h" > ... > Widgets Widget ; Correct. > however, given the inclusion of the header file, doesn't this give me > both a referencing declaration and a defining declaration of that > object in the same file? It gives you both a declaration and a definition in the same compilation unit. > is there no problem with that? No. If there was, you wouldn't be able to have forward declarations, and thus wouldn't be able to define mutually-recursive functions. > i was under > the impression that common practise was to have a single defining > declaration and all the *remaining* be referencing declarations. You shouldn't define a symbol more than once in any compilation unit, but you can declare it as many times as you like. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:27 ` Robert P. J. Day 2005-09-06 19:45 ` Robert P. J. Day @ 2005-09-07 1:17 ` Glynn Clements 1 sibling, 0 replies; 9+ messages in thread From: Glynn Clements @ 2005-09-07 1:17 UTC (permalink / raw) To: Robert P. J. Day; +Cc: nhorman, C programming list Robert P. J. Day wrote: > > > i'm looking at some legacy code and, in a header file, i find the > > > following (paraphrased for brevity): > > > > > > typedef struct { > > > ... stuff ... > > > } Widgets ; > > > > > > extern Widgets Widget ; > > > > > > > > > huh? i can see why a header file would want to define a structure > > > but i'm confused why the *header* file would then refer to an external > > > object of that type. that's a new one on me -- typically, i'd expect > > > a *source* file to define such a thing and other *source* files to > > > contain the "extern" declaration. > > > > > > is this some subtle programming cleverness of which i am unaware? > > > thanks. > > > > This is done quite frequently when a data structure needs to be > > referenced from multiple locations. They're not necessicarily > > consecutive like that, but its rather a common practice to extern a > > instance of a type in a header file. The kernel source tree does > > this very frequently, check include/net/ipv4 for lots of examples. > > ok, so what it represents is just a shortcut so that each source file > doesn't have to have its own extern, it just comes along with the > header file. Yep. > all right, i guess i can live with that although it strikes me as > just a little bit sleazy. oh, well. Huh? That's what header files are for. Or do you have stuff like: extern int strcmp(const char *s1, const char *s2); at the top of all of your source files? -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:23 ` nhorman 2005-09-06 19:27 ` Robert P. J. Day @ 2005-09-06 19:57 ` Steve Graegert 1 sibling, 0 replies; 9+ messages in thread From: Steve Graegert @ 2005-09-06 19:57 UTC (permalink / raw) To: nhorman@tuxdriver.com; +Cc: Robert P. J. Day, C programming list On 9/6/05, nhorman@tuxdriver.com <nhorman@tuxdriver.com> wrote: > On Tue, Sep 06, 2005 at 03:13:48PM -0400, Robert P. J. Day wrote: > > > > i'm looking at some legacy code and, in a header file, i find the > > following (paraphrased for brevity): > > > > typedef struct { > > ... stuff ... > > } Widgets ; > > > > extern Widgets Widget ; > > > > > > huh? i can see why a header file would want to define a structure > > but i'm confused why the *header* file would then refer to an external > > object of that type. that's a new one on me -- typically, i'd expect > > a *source* file to define such a thing and other *source* files to > > contain the "extern" declaration. > > > > is this some subtle programming cleverness of which i am unaware? > > thanks. > > > This is done quite frequently when a data structure needs to be referenced from > multiple locations. They're not necessicarily consecutive like that, but its > rather a common practice to extern a instance of a type in a header file. The > kernel source tree does this very frequently, check include/net/ipv4 for lots of > examples. That's correct. It is also used to implement some kind of inheritance in C as can be seen in the Motif code. Here, widgets are organized in a "class hierarchy" and every widget inherits features of it "super class" by simply defining new "parts" in separate structures that are combined with the super class' part structure into a record which is then defined extern for the reasons Neil just mentioned. This leads to these weird structure definitions you encountered. Regards \Steve -- Steve Graegert <graegerts@gmail.com> Software Consultancy {C/C++ && Java && .NET} Mobile: +49 (176) 21248869 Office: +49 (9131) 7126409 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: weird structure definition in header file 2005-09-06 19:13 weird structure definition in header file Robert P. J. Day 2005-09-06 19:23 ` nhorman @ 2005-09-06 20:24 ` Ronaldo.Afonso 1 sibling, 0 replies; 9+ messages in thread From: Ronaldo.Afonso @ 2005-09-06 20:24 UTC (permalink / raw) To: Robert P. J. Day; +Cc: C programming list, linux-c-programming-owner I'm not sure if I understood your doubt but just think a little bit, you can put all the variables declaration inside a header file, be those variables extern or not, and your source.c file (or source.c files) could just "include" that header file. Beside of this, you could have other modules where those variables are "really" declared. A extern variable informs to the compiler that this variable is not is that module, just that. The link must do the hard work. I hope this information worths for you. Good luck. Ronaldo Z. Afonso Projetista de Software Cyclades Brasil ronaldo.afonso@cyclades.com.br Phone: 55 11 5033-3361 Fax: 55 11 5033-3388 www.cyclades.com.br "Robert P. J. Day" <rpjday@mindsprin To g.com> C programming list Sent by: <linux-c-programming@vger.kernel.or linux-c-programmi g> ng-owner@vger.ker cc nel.org Subject weird structure definition in 09/06/2005 04:13 header file PM i'm looking at some legacy code and, in a header file, i find the following (paraphrased for brevity): typedef struct { ... stuff ... } Widgets ; extern Widgets Widget ; huh? i can see why a header file would want to define a structure but i'm confused why the *header* file would then refer to an external object of that type. that's a new one on me -- typically, i'd expect a *source* file to define such a thing and other *source* files to contain the "extern" declaration. is this some subtle programming cleverness of which i am unaware? thanks. rday - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-09-07 1:20 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2005-09-06 19:13 weird structure definition in header file Robert P. J. Day 2005-09-06 19:23 ` nhorman 2005-09-06 19:27 ` Robert P. J. Day 2005-09-06 19:45 ` Robert P. J. Day 2005-09-06 19:56 ` nhorman 2005-09-07 1:20 ` Glynn Clements 2005-09-07 1:17 ` Glynn Clements 2005-09-06 19:57 ` Steve Graegert 2005-09-06 20:24 ` Ronaldo.Afonso
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).