Node: C++ Interface, Next: , Previous: Vague Linkage, Up: C++ Extensions

Declarations and Definitions in One Header




The header file contains the full definitions, but is marked with #pragma interface in the source code. This allows the compiler to use the header file only as an interface specification when ordinary source files incorporate it with #include. In the single source file where the full implementation belongs, you can use either a naming convention or #pragma implementation to indicate this alternate use of the header file.

#pragma interface
#pragma interface "subdir/objects.h"
Use this directive in header files that define object classes, to save space in most of the object files that use those classes. Normally, local copies of certain information (backup copies of inline member functions, debugging information, and the internal tables that implement virtual functions) must be kept in each object file that includes class definitions. You can use this pragma to avoid such duplication. When a header file containing #pragma interface is included in a compilation, this auxiliary information will not be generated (unless the main input source file itself uses #pragma implementation). Instead, the object files will contain references to be resolved at link time.


#pragma implementation
#pragma implementation "objects.h"
Use this pragma in a main input file, when you want full output from included header files to be generated (and made globally visible). The included header file, in turn, should use #pragma interface. Backup copies of inline member functions, debugging information, and the internal tables used to implement virtual functions are all generated in implementation files.

If you use #pragma implementation with no argument, it applies to an include file with the same basename1 as your source file. For example, in, giving just #pragma implementation by itself is equivalent to #pragma implementation "allclass.h".

In versions of GNU C++ prior to 2.6.0 allclass.h was treated as an implementation file whenever you would include it from even if you never specified #pragma implementation. This was deemed to be more trouble than it was worth, however, and disabled.

If you use an explicit #pragma implementation, it must appear in your source file before you include the affected header files.

Use the string argument if you want a single implementation file to include code from multiple header files. (You must also use #include to include the header file; #pragma implementation only specifies how to use the file--it doesn't actually include it.)


#pragma implementation and #pragma interface also have an effect on function inlining.

If you define a class in a header file marked with #pragma interface, the effect on a function defined in that class is similar to an explicit extern declaration--the compiler emits no code at all to define an independent version of the function. Its definition is used only for inlining with its callers.



  1. A file's basename was the name stripped of all leading path information and of trailing suffixes, such as .h or .C or .cc.