Skip to content

Instantly share code, notes, and snippets.

@jdarpinian
Last active February 6, 2018 12:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdarpinian/84a28a1ed8a36313a4e0cad8b3cd347f to your computer and use it in GitHub Desktop.
Save jdarpinian/84a28a1ed8a36313a4e0cad8b3cd347f to your computer and use it in GitHub Desktop.
Directly execute C source code by embedding in a bash script.
#!/bin/bash
COMPILER_OPTIONS="-g -Wall -Wextra --std=c11 -O1 -fsanitize=address,undefined"
SOURCE_CODE=$(cat <<'END_OF_SOURCE_CODE'
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
END_OF_SOURCE_CODE
);THIS_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)/$(basename "${BASH_SOURCE[0]}")"
OUT_FILE="/tmp/build-cache/$THIS_FILE"; mkdir -p "$(dirname "$OUT_FILE")"; set -e;
test "$THIS_FILE" -ot $OUT_FILE || $(which clang || which gcc) $COMPILER_OPTIONS -xc -o "$OUT_FILE" - << END_OF_COMPILER_INPUT
#line 4 "$THIS_FILE"
$SOURCE_CODE
END_OF_COMPILER_INPUT
exec -a "$0" "$OUT_FILE" "$@"
@jdarpinian
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment