Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!agate!bionet!apple!bloom-beacon!mit-eddie!uw-beaver!uw-june!uw-entropy!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c++ Subject: Re: Managing C++ Libraries: Dependencies and Headers Message-ID: <1739@dataio.Data-IO.COM> Date: 7 Nov 88 19:09:54 GMT References: <5078@thorin.cs.unc.edu> Reply-To: bright@dataio.Data-IO.COM (Walter Bright) Organization: Data I/O Corporation; Redmond, WA Lines: 99 While the scheme presented is interesting, I find it unnecessarilly complex. Here's the scheme I used which satisfies the basic requirements: 1. In each header file, put a 'wrapper' around it of the form: // This is foo.hpp #ifndef FOO_HPP #define FOO_HPP ... // text of header file #endif // FOO_HPP 2. In the text of each header file, for each header that it is dependent on, add the following: #ifndef DEPEND_HPP #include "depend.hpp" #endif 3. In the application code, just insert #includes for the classes or definitions that are *used*, the dependent ones will be automatically included. Using this scheme requires minimal discipline, it doesn't require unusual .d files, or awk scripts. It's only disadvantage is that sometimes causes a header file to be scanned twice (but never more than twice). Here's an example: Class abc depends on class def. Class def depends on ghi and jkl. Class mno depends on ghi. The application code has instances of abc, mno and def. The files look like: // abc.hpp #ifndef ABC_HPP #define ABC_HPP #ifndef DEF_HPP #include "def.hpp" #endif .... // text of definition #endif // ABC_HPP // def.hpp #ifndef DEF_HPP #define DEF_HPP #ifndef GHI_HPP #include "ghi.hpp" #endif #ifndef JKL_HPP #include "jkl.hPP" #endif .... // text of definition #endif // DEF_HPP // ghi.hpp #ifndef GHI_HPP #define GHI_HPP .... // text of definition #endif // GHI_HPP // jkl.hpp #ifndef JKL_HPP #define JKL_HPP .... // text of definition #endif // JKL_HPP // mno.hpp #ifndef MNO_HPP #define MNO_HPP #ifndef GHI_HPP #include "ghi.hpp" #endif .... // text of definition #endif // MNO_HPP The application code source file looks like (recall that abc, def and mno were used): #include "abc.hpp" #include "mno.hpp" #include "def.hpp" Note that these #includes can be done in any order. The application code is not cluttered with #includes or #ifndef..#endif pairs. The correct dependent .hpp files are automatically #included in the correct order. Note also that the above will #include def.hpp twice. This is the worst case, it's only effect is to slow the compilation by the time it takes the preprocessor to scan it (because of the 'wrapper'). I don't believe this is a serious limitation.