Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use Conditional Compilation to Hide Platform Dependencies and Implement #include Guards-2 : Page 2


advertisement
Demonstrating the Problem
Suppose you're developing an application that runs on both 32- and 64-bit environments. The differences between these two platforms aren't trivial: the representation of pointers under each platform is different as are the alignment requirements, and the sizes of built-in types. However, the project is essentially the same on both platforms so you don't want to maintain two distinct versions of each source file. In addition, you need to organize your project in an easy to maintain set of self-contained units. The preprocessor can simplify these tasks.

#include Guards
Even if you're not planning for cross-platform development, every non-trivial project consists of one or more units. A unit is a pair of .cpp and .h files that each contain a logical entity such as a class, a function, a template, an enum type, etc. Usually, the .cpp file contains the implementation, whereas the .h file contains only the declarations. The following Coord class exemplifies this. Its declaration appears in a header file and its members are defined in a matching .cpp file:

// file Coord.h class Coord { private: int _x; int _y; static int counter; public: explicit Coord (int x=0, int y=0); int getX () const; int getY () const; }; // file Coord..cpp #include "Coord.h" Coord::Coord(int x, int y): _x(x), _y(y) {} int Coord::getX () const { return _x; } int Coord::getY () const { return _y; } int Coord::counter=0; //define the static member

The first problem in this code is that Coord.h might be #included multiple times during the same compilation session. To avert this, use an "#include guard. An #include guard is essentially a macro flag. When it isn't set, this means that the header is being #included for the first time and should therefore be visible to the compiler. Otherwise, the header's content is hidden from the compiler by macro magic. An #include guard should appear at the first line of every header file:


// file Coord.h #if !defined (COORD_H)//#included for the first time? #define COORD_H //then set the flag and make the rest //of this header visible to the compiler class Coord { private: int _x; int _y; public: explicit Coord (int x=0, int y=0); int getX () const; int getY () const; }; #endif //COORD_H

Don't forget to add a matching #endif at the bottom of the file. Notice that you don't have to specify a meaningful value for the flag. The flag's name usually consists of the header's name and _H attached to it. Of course, you may choose any other convention that ensures uniqueness and clarity. From now on, Coord.h will be #included at most once in every compilation session.

Note: All standard headers already have #include guards in them so you don't need to add them manually to these headers.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap