C++: #pragma implementation/interface

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

C++: #pragma implementation/interface

Post 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.
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C++: #pragma implementation/interface

Post 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.)
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:C++: #pragma implementation/interface

Post by Neo »

I did know that prgamas are complier specific.
Actually I was trying to find out what #pragma interface and #pragma implementation do?
Only Human
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C++: #pragma implementation/interface

Post 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
Every good solution is obvious once you've found it.
Post Reply