Skip to content

Instantly share code, notes, and snippets.

@nilput
Last active May 31, 2020 09:59
Show Gist options
  • Select an option

  • Save nilput/8e2f75edf43607c065fa033f020bef74 to your computer and use it in GitHub Desktop.

Select an option

Save nilput/8e2f75edf43607c065fa033f020bef74 to your computer and use it in GitHub Desktop.
a script to compile and execute C snippets
#!/bin/bash
function die() {
echo "$*" >&2
[ -n "$tmp" -a -d "$tmp" ] && rm -rf "$tmp"
exit 1;
}
tmp="$(mktemp -d -t cexec-XXXXXX)" && [ -n "$tmp" -a -d "$tmp" ] || die "couldn't create tmp dir";
[ "$1" = '-h' ] && die "$(printf "Usage: $0 <<<'C Source code")"
cat <(printf "#include <%s.h>\n" stdio stdlib string) \
<(printf 'int main(int argc,char *argv[]){') \
- \
<(echo "}") | gcc -o "$tmp/prog" -x c - || die "compilation failed" && "$tmp"/prog "$@"
die 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment