Self-building C files (on *nix) ------------------------------- I came up with this scheme while just messing around, so it's probably not for you. But it does let you make .c an executable and just run it. It started as a simple one liner of POSIX shell but I extended it into a separate script that you can just source into the (correctly formatted) .c file. == How does it work? It abuses C preprocessor, shell and old UNIX heritage to run shell scripts. See my blog post about it: [TODO: write the blogpost] Tested in: dash, bash, zsh. == How do I use it? TL;DR See main.c. Put build.sh into your working directory, then put this at the beginning of your C file: #if 0 . build.sh exit #endif /* Your C code here */ Make your C file executable (chmod +x $cfile). VoilĂ , now just run it! You can configure basic things with environment variables, be it in your C file, or from the calling shell. For example: $ DEBUG=no ./$cfile --- cc time: .0000001 sec --- debug=no; static=yes --- Program output: sh: Segmentation fault. Core dumped. $ _ Hey now I just joke about your code crashing, come back! Crashing early is a good thing... T_T For those who are still with me, there are several environment variables that you can use: * CC - what compiler to use (default: "cc" aka the default of the system) * STATIC - yes/no, compile as static executable (default: "yes") * DEBUG - yes/no, compile with DBGFLAGS if yes, compile with RELFLAGS and strip the executable if yes (default: "yes") * RUN - yes/no, run the program after build? (default: "yes") * OUT - name of resulting executable (default: "a.out") * CFLAGS - flags to pass to the compiler (default: "-Wall -Wextra") * DBGFLAGS - debug flags to pass to the compiler when DEBUG=yes (default: "-g -Og") * RELFLAGS - release flags to pass to the compiler when DEBUG=no (default: "-O2") If you want explanations for my weird default choices read the blogpost [TODO: write the blogpost]. The defaults do assume gcc like compiler, you are of course free to change defaults in your copy of the script. Note when changing CC and *FLAGS you'll need to "clean" your project by deleting the executable (rm $OUT). Changing STATIC and DEBUG is detected for you and all is well. == Fun fact This README is longer than the build.sh script itself. Go read it! It's meant to be tinkered with to make it your own. == Why? Because I wanted to. == Who? Literally me.