Last Updated: February 25, 2016
·
1.168K
· Tarcísio Xavier Gruppi

#!gcc - run c like a script language

Following the ideia on this post I tried to run C code using the same process and this is the result!

//bin/cat /dev/null; FILE="`mktemp -u -t bin`"; gcc -o "$FILE" "$0" && "$FILE" "$@"; exit

#include <stdio.h>

int main(int argc, char *argv[]) {
  int i;
  printf("Printing arguments.\n");
  for (i = 0; i < argc; i++) {
    printf("%d: %s\n", i, argv[i]);
  }
}

1 Response
Add your response

A great enhancement to this would be to detect changes in the file and only recompile when there have been changes. Could do this by hashing the file, naming the output off gcc using the hash, and not executing gcc again if the output file exists.

over 1 year ago ·