Node:C++ Interface, Next:Template Instantiation, Previous:Vague Linkage, Up:C++ Extensions
C++ object definitions can be quite complex. In principle, your source code will need two kinds of things for each object that you use across more than one source file. First, you need an interface specification, describing its structure with type declarations and function prototypes. Second, you need the implementation itself. It can be tedious to maintain a separate interface description in a header file, in parallel to the actual implementation. It is also dangerous, since separate interface and implementation definitions may not remain parallel.
With GNU C++, you can use a single header file for both purposes.
Warning: The mechanism to specify this is in transition. For the nonce, you must use one of two
#pragmacommands; in a future release of GNU C++, an alternative mechanism will make these
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
#pragma implementation to indicate this alternate
use of the header file.
#pragma interface "subdir/objects.h"
#pragma interfaceis 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.
The second form of this directive is useful for the case where you have
multiple headers with the same name in different directories. If you
use this form, you must specify the same string to
#pragma implementation "objects.h"
#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
allclass.cc, giving just
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
allclass.cc even if you never specified
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;
implementation only specifies how to use the file--it doesn't actually
There is no way to split up the contents of a single header file into multiple implementation files.
#pragma implementation and
#pragma interface also have an
effect on function inlining.
If you define a class in a header file marked with
interface, the effect on a function defined in that class is similar to
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.
Conversely, when you include the same header file in a main source file
that declares it as
#pragma implementation, the compiler emits
code for the function itself; this defines a version of the function
that can be found via pointers (or by callers compiled without
inlining). If all calls to the function can be inlined, you can avoid
emitting the function by compiling with
If any calls were not inlined, you will get linker errors.
A file's basename
was the name stripped of all leading path information and of trailing
suffixes, such as