7.6 Compiling CSS files as C++ Hard Code

Because they use the standard C++ syntax, CSS script files can be compiled into "hard" code that runs (fast) as a stand-alone application. Of course, the files must not use any of the CSS shortcuts, and must otherwise be standard C++ code (i.e., no executable code outside of functions, using the correct . or -> operator, etc).

There are three main steps that are needed to compile your CSS code. The first, which need only be done once, is the creation of the appropriate libraries that will be linked with the C++ compiled code to produce an executable. The second is formatting your file so that it can be both run by CSS and compiled by C++. The third is creating a Makefile which will allow C++ to compile your file. An example of this is provided in the directory `demo/css'.

There are two special libraries that are linked into your C++ executable, one in the `src/ta' directory, and one in the `src/css' directory. Both can be made using top-level Makefile commands, or by going into the directories separately. `make LibMin' makes a library which contains the minimal type-access stuff from the `src/ta' directory of the distribution, and it makes the `libtypea_min' library. `make hard_css' makes a library of special functions in `src/css' (e.g., the "special math" functions which have been added into CSS and are not part of the standard C library, and the Dir and ReadLine functions). This library is `libhard_css'. Both of these libraries will be visible to the C++ compiler using the makefiles as described below.

The CSS file needs to have a couple of conditially-included elements that resolve the basic differences between CSS and C++. Basically, this amounts to including a header file that estabilshes some defines and includes some commonly-used standard library headers, which are automatically present in CSS. This is the `css/hard_of_css.h' file. It is only included when compiling by making it conditional on the pre-processor define __CSS__, which is automatically defined by CSS. Also, `hard_of_css.h' defines a main function which calls a function called s_main, which is the actual main function that should be defined in your script.

The following example illustrates these elements, and can be used as a template for making your own CSS files compilable (see `demo/css' for a larger example):

#ifndef __CSS__
#include <css/hard_of_css.h>
#endif

void s_main(int ac, String* av) {
  // do stuff here..
}

// in css, call our main function as the thing we actually run..
#ifdef __CSS__
s_main(argc, argv);
#endif

In order to make the C++ compiling environment as similar to CSS as possible, a variant of the same Makefile can be used. This assumes that the makefiles for your CPU type are correct (i.e., those used in installing the PDP++/CSS source-code distribution (see section 2 Installation Guide, section 2.6 Installing the Programmers Version)). The following steps will result in a Makefile that will enable you to compile your CSS code.

1) Copy the sample makefile in `config/Makefile.hard_of_css' into the directory where your CSS file is to be compiled, and name it `Makefile.in'.

2) Edit this file and ensure that the PDPDIR path is pointing to the installed pdp++ distribution.

3) Then, do a make -f Makefile.in InitMakefile, which will make a `Makefile' in the current directory that can be used to compile your file.

4) To compile, just type make <filenm>, where <filenm> is the CSS file without any extension (i.e., the name of the executable that will be produced. Some C++ compilers will complain if the file does not end in a "standard" C++ extension like .cc or .C, so you may have to rename it or create a symbolic link from your .css file (CSS does not care about using a non .css extension, as long as you specify the entire file name).