Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I do something similar (but less portable and more verbose) in C++ sometimes when I want to prototype something. My boilerplate is something like this:

  #if 0
  TMP=$(mktemp -d);
  c++ -std=c++11 -o ${TMP}/a.out ${0} && ${TMP}/a.out ${@:1}; RV=${?};
  rm -rf ${TMP};
  exit ${RV};
  #endif
    
  #include <iostream>
    
  int main()
  {
    std::cout << "Hello, world!\n";
  }
(the trailing semi-colons in the script part is to make my editor indent the C++ code properly)


HA! I was about to share my way, but you seem to have done more or less what I have in C instead!

I was having fun with old Golang C code before they started bootstrapping in Go and have found they were using interpunct to separate function names, so I decided to play around with shellscript-like C code to goof around:

        #if 0
        set -e; [ "$0" -nt "$0.bin" ] &&
        gcc -O2 -Wall -Wextra -Wpedantic -std=c11 "$0" -o "$0.bin" -s
        exec "$0.bin" "$@"
        #endif

        #include <stdio.h>

        void runtime·sysAlloc(void);

        int main(void)
        {
            unsigned age = 44;
            printf("I am %u years old.\n", age);
            runtime·sysAlloc();
            return 0;
        }

        void runtime·sysAlloc(void) {
            printf("Hello from C code, %d!\n", 90);
        }


That's exactly how I keep my quick test C or C++ programs. I like how one can keep all the compilation options in the same file this way.


I love the opening #if 0. So many tricks from multilingual quines, maybe they can actually be useful :)


I absolutely love this, super clever!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: