cgo
an example compileAndGo script using C

Wrap C code in a compileAndGo script, as in the example below. Once-only compilation is automatic; the executable file is cached, and the source is right there in the executable script.

Example:

1 Z% ./cgo 'a b' c
[ /usr/bin/gcc -O2 -o /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo.c ]
[ /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo a b c ]
Hello, folks of World!
Invoked as: /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo
Arguments are: "a b" "c"
2 Z% ./cgo 'a b' c
[ /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo a b c ]
Hello, folks of World!
Invoked as: /Users/yost/tmp/CompileAndGo/cgo/Darwin-Power-Macintosh/2005-07-27_00-20-51_-usr-bin-gcc/cgo
Arguments are: "a b" "c"
3 Z% 

Here’s the cgo script.

#!/usr/bin/env compileAndGo
# See http://Yost.com/computers/compileAndGo/cgo.html
language    = c
commandName = cgo
compiler    = /usr/bin/gcc
verbose     = 2
# Other variables default as shown.
#firstArgIsCommandName = false
#sourceFilename     = $commandName.c
#executableFilename = $commandName
#compilerArgs       = -o $cacheDir/$executableFilename $cacheDir/$sourceFilename
#execute            = $cacheDir/$executableFilename
!#

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
  int ind;
  printf("Hello, folks of World!\n");
  printf("Invoked as: %s\n", argv[0]);
  printf("Arguments are:");
  for (ind = 1; ind < argc; ++ind) {
    printf(" \"%s\"", argv[ind]);
  }
  printf("\n");
  exit(0);
}

(“Hello, folks of World!” is the Goon Show variant of “Hello, world!”)


http://Yost.com/computers/compileAndGo/cgo.html - this page
2004-03-03 Created
2005-07-24 Modified to use the new compileAndGo program
2005-07-25 Modified
2005-07-26 Modified with workaround for broken shells
2005-07-27 Modified
2005-11-06 End the header with !# per compileAndGo 5.0