Last active
February 6, 2018 12:17
-
-
Save jdarpinian/84a28a1ed8a36313a4e0cad8b3cd347f to your computer and use it in GitHub Desktop.
Directly execute C source code by embedding in a bash script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better version here: https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83a7aa4e2