Page 1 of 1
C++: #pragma implementation/interface
Posted: Thu Jun 01, 2006 11:26 pm
by Neo
I was trying to understand what these mean.
Code: Select all
#pragma interface
#pragma implementation
I was able to find info on the
net but it isn't really very enlightining.
Has anybody got some better info? I will post if I find anything more.
Re:C++: #pragma implementation/interface
Posted: Fri Jun 02, 2006 12:54 am
by Solar
Everything that follows a [tt]#pragma[/tt] is implementation defined, i.e. specific to a certain compiler (version). (Exception: C99 defines a couple of things regarding floating point arithmetics that can be controlled with [tt]#pragma STD ...[/tt], but they're pretty esoteric.)
Re:C++: #pragma implementation/interface
Posted: Fri Jun 02, 2006 12:59 am
by Neo
I did know that prgamas are complier specific.
Actually I was trying to find out what #pragma interface and #pragma implementation do?
Re:C++: #pragma implementation/interface
Posted: Fri Jun 02, 2006 1:32 am
by Solar
Consider MyClass.hpp, MyClass.cpp, and OtherSource.cpp.
MyClass.cpp is the implementation of the class declared in MyClass.hpp. OtherSource.cpp is a client, i.e. it uses MyClass objects in some way.
That means that MyClass.hpp is used in two different contexts.
The idea the GCC people came up with is that MyClass.hpp should contain [tt]#pragma interface[/tt], so that when compiling OtherSource.cpp, the compiler can omit some of the stuff that is required by MyClass.cpp only (backup copies of inline member functions, debugging information, and the internal tables that implement virtual functions), which will then be resolved at link time.
MyClass.cpp, in turn, should contain [tt]#pragma implementation[/tt] to tell the compiler it needs the whole thing, not the cut-back version.
It saves a bit of temporary disk space, perhaps a bit of final executable size (but not much if the linker isn't completely braindead), might cut back a bit on compilation times - and opens a whole new can of worms in the "strange errors" department, is documented to be work in progress, and could do the gods know what when compiled on a different compiler.
The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue.
-- Richard M. Stallman, The GNU C Preprocessor, version 1.34