next up previous contents
Next: 2.4 G++ 2.7.2 Up: 2 Compiling And Running Previous: 2.2 Avoiding Name Collisions

2.3 Compiling Templates

  This software should compile correctly with either g++ 2.5.8 or g++ 2.6.3, as long as the proper flags are used ( -fno-implicit-templates and -DDONT_INCLUDE_TEMPLATE_CODE for g++ 2.6.3). We take advantage of some of the new features of g++ 2.6.3 to ensure that the code for templates is not duplicated (this reduces the size of the executable by about ). This makes the compilation a bit more complicated: this section describes some of the techniques we use to make it work reasonably well.

A parameterized class's declaration is in a `` .h '' file and the code for its member functions in a `` .c '' file. The `` .c '' files for parameterized classes are kept in the include directory, as they are #include -d rather than compiled separately. Each class' `` .h '' file also contains a macro that expands to list all the other templates associated with a template you might be using. These macros, including those for types used by the Omega Library, should all be used expanded in one file, to avoid redundant definitions. The file `` include/omega/PT-omega.c '' contains a list of all the templates used by the Omega Library, and can be used to simplify the construction of a single file that contains all parameterized types. For example, the file `` PT.c '' is used in the compilation of the Omega Calculator: It #include s omega/PT-omega.c and explicitly lists the other parameterized types used by the calculator.

The functions of these features depend on the compiler being used:

G++ 2.5.8

The declaration and definition of all template functions must be given in every header file. To accomplish this, the header file for a given class #include s the `` .c '' file with the code for that class's member functions. The `` .c '' file should not be compiled directly. `` PT-omega.c '' and `` PT.c '' are not needed - fortunately g++ 2.5.8 ignores their contents.

G++ 2.6.3

The definitions of the external template functions must not be included in multiple source files. We therefore use -fno-implicit-templates and -DDONT_INCLUDE_TEMPLATE_CODE when compiling - the latter prevents inclusion of the `` .c '' files by the headers. The file `` PT.c '' #undef s DONT_INCLUDE_TEMPLATE_CODE before including the headers, so it will get the function definitions. It #include s `` PT-omega.c '', so this one source file will contain a copy of each template needed anywhere in the Omega Calculator. Note that our instantiate macros contain some redundancies, and the compiler will issue warnings about them, so warnings for PT.c

are turned off in the makefile.



next up previous contents
Next: 2.4 G++ 2.7.2 Up: 2 Compiling And Running Previous: 2.2 Avoiding Name Collisions



omega@cs.umd.edu