|
6. System Buildingrevised 15 Feb 2001 |
My own C++ code has to fit into a project. This page talks about practices that should make it fit better.
My practice has always been to compile with the highest possible warning level, and to treat all warnings as errors. Different compilers have different options to indicate this.
Unfortunately, one of the leading implementations makes this difficult to impossible. If you're using MSVC++, you may want to have a look at Microsoft vs. C++ for some details on the problems and what limited measures I've evolved to deal with them.
gronk.h
with
#ifndef GRONK_H_ #define GRONK_H_and end it with
#endif // GRONK_H_[ 2000-03-03 -- to next update]
[ 2000-03-03
-- to next update]
The above internal include guards are pretty much necessary
for safety. But in February 2000 there was an interesting discussion
about external include guards on comp.lang.c++.moderated. An
external guard puts the #ifndef-#endif
pair outside the
#include
, like this:
#ifndef GRONK_H_ #include "gronk.h #define GRONK_H_ #endifAs you see, the
#define
follows the
#include
, so there's no interference with an internal
include guard. The potential benefit of external guards is that the
compiler never even has to open the include file a second time, and on
some compilers this significantly speeds up build times.
So internal guards are a necessary safety measure, and external guards might speed things up. Why not use both? Well, some compilers don't build significantly faster with external guards; and external guards do add a maintenance burden, however slight. The strongest reason is that some compilers already recognize and skip a reused header file, and in the future probably more will do so. At present, whether to use external guards depends on the compiler, the environment, and the size of the project.
#include
s in header files.
Forward declarations can also help to reduce
header-file nesting.
class B;instead of
#include "b.h"This means that when b.h changes, I won't have to recompile everything that includes a.h, because a.h no longer includes b.h. The forward declaration is also sufficient for functions that return a B or take a B passed by value:
class B; B normalized(B rawEntry); // compiles just fine[ 2000-06-16 -- to next update]
contact info | site map | |
this page:
http://oakroadsystems.com/codeprac/build.htm |