Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 1415-410, S’04- 1 -#includeFeb. 6, 2004Dave EckhardtDave EckhardtBruce MaggsBruce MaggsL12a_include15-410“...#ifndef DSFLK_FSFDDS_FSDFDS...”15-410, S’04- 1 -SynchronizationRegister your partner todayRegister your partner todayRegister your partner todayRegister your partner todayRegister your partner todayRegister your partner todayHow is P2 going?How is P2 going?–Should have read both handout documents by now–Should start thr_create() before Monday15-410, S’04- 1 -Outline#ifndef DSFLK_FSFDDS_FSDFDS#ifndef DSFLK_FSFDDS_FSDFDSWhat What shouldshould go here, anyway? go here, anyway?15-410, S’04- 1 -What's _STDIO_H_ anyway?#ifndef _STDIO_H_#define _STDIO_H_typedef struct FILE { ...} ...;#endif /* _STDIO_H_ */15-410, S’04- 1 -ArchaeologyC is oldC is oldC doesn't have modulesC doesn't have modulesC has C has filesfiles–Compilers sort of know some file types: .c, .s–Compilers don't really know about .h●Auxiliary “pre-processor” brain hides themPeople use People use conventionsconventions to get module-like C to get module-like C–These conventions evolved slowly15-410, S’04- 1 -The “.h Responsibility” DilemmaAssume: “stdio module”Assume: “stdio module”Assume: “network stack module”Assume: “network stack module”–(Trust us, it's modular!)Both need to knowBoth need to know–What's a size_t on this machine, anyway?–#include <sys/types.h>15-410, S’04- 1 -Nested ResponsibilityProgram 1:Program 1:–#include <stdio.h>Program 2:Program 2:–#include <netinet/tcp_var.h>AssumeAssume–Program 1, 2 don't need sys/types.h themselvesSolution 1Solution 1–stdio.h and netinet/tcp_var.h each include sys/types.h15-410, S’04- 1 -Too MuchProgram 3:Program 3:–#include <stdio.h>–#include <netinet/tcp_var.h>ProblemProblem–Now we get two copies sys/types.h–Lots of whining about redefinitions–Maybe compilation fails15-410, S’04- 1 -Passing the BuckBlame the user!Blame the user!Solution 2Solution 2–Require main program to #include <sys/types.h>ProblemProblem–Annoying for user–Modules' needs change over time●Didn't you know? Since last night xxx needs yyy...15-410, S’04- 1 -Solution: Idempotent .h files.h responsibility.h responsibility–Activate only once–No matter how many times included–Choose string “unlikely to be used elsewhere”#ifndef _STDIO_H_#define _STDIO_H_...#endif /* _STDIO_H_ */15-410, S’04- 1 -What Belongs In a .h?Types (C: Types (C: declarationsdeclarations, not , not definitionsdefinitions))Exported interface routines (“public methods”)Exported interface routines (“public methods”)Constants (#define or enum)Constants (#define or enum)Macros (when Macros (when appropriateappropriate))Data items exported by moduleData items exported by module–Try to avoid this–Same reason as other languages: data != semanticsNo code!No code!15-410, S’04- 1 -But What About...?Real modules have multiple .c filesReal modules have multiple .c files–Who declares internal data structures?●(Internally, we agree on semantics)–Who declares internal functions?Not “the” .h fileNot “the” .h file–We don't want to publish internal detailsMaybe a “.i” file?Maybe a “.i” file?–Help?15-410, S’04- 1 -Use the Other .h File!stdio.hstdio.h–Included by module clients–Included by module partsstdio_private.hstdio_private.h–Included only by module parts–Ideally, not available to user's prying eyes*_private.h should be idempotent, too*_private.h should be idempotent, too15-410, S’04- 1 -Summary#ifndef DSFLK_FSFDDS_FSDFDS#ifndef DSFLK_FSFDDS_FSDFDS–Well, use a better string–Used to make .h files idempotentWhat What shouldshould go here, anyway? go here, anyway?–There are two “here”'s here●foo.h: public interface, available to public●foo_private.h: internal communication, maybe
View Full Document