www.digitalmars.com [Home] [Search] [Contents]

Tips For Precompiled Headers

  1. Header files should contain complete definitions, i.e. starting a declaration in one file and finishing it in another should be avoided.
  2. Design headers so they only need to be #include'd once. Then, wrap the header in:
    	#ifndef _HEADER_H
    	#define _HEADER_H 1
    		... declarations ...
    	#endif // _HEADER_H
    
    so that if it is #include'd multiple times, it is parsed only once. In addition, add:
    	#pragma once
    
    to it so the compiler doesn't need to rescan it.
  3. Design headers so they can be #include'd in any order. The best way to do this is to have each header #include the ones it depends on. By following rule (2), this will not have a compile time penalty.
  4. Avoid defining data in the headers.
  5. Avoid #undef'ing macros and redefining them to something else.
  6. If you set the struct packing to something other than the default, set it back to the default before the end of the file.