Next: , Previous: , Up: C   [Index]


Externs

Externs

Ctalk provides a few facilities to help when compiling code in several input modules.

There are also a few caveats when dealing with C variables in multiple input modules, which are described below.

Ctalk allows you to prototype methods. That is, you can declare a method in a source code module before compiling another module later where the method is actually defined.

Prototypes are similar to method declarations, except that the prototype omits the method body. For example, a method prototype before the method is first used would look like this.

String instanceMethod myLength (void);

You can also define a different return class in the prototype, as in this example.

String instanceMethod myLength (void) returnClass Integer;

For example, if the input file module1.ca looks like this:


String instanceMethod trimStrLength (void) returnObjectClass Integer;

int main () {

  String new myStr;

  myStr = "Hello, world!";

  printf ("%s\n", myStr subString 0, myStr trimStrLength 2);
}

and the file module2.ca, which contains the definition of trimStrLength, looks like this:


String instanceMethod trimStrLength (void) {
  returnObjectClass Integer;
  return self length - 1;
}

Then you can build the program with a command line like the following, and Ctalk will have the definition of trimStringLength while compiling module1.ca, before it actually compiles the method in module2.ca.


$ ctcc module1.ca module2.ca -o myprog

C Variables and extern Declarations

When using a global C variable in several input modules, you only need to declare it once, before it is first used. Ctalk combines the C code of all of the input files with one copy of the class libraries, so it isn’t necessary to declare a C variable in the first module and then declare it as extern in the modules that get compiled later.


Next: , Previous: , Up: C   [Index]