Skip to content

Instantly share code, notes, and snippets.

@aprell
Last active October 6, 2019 08:28
Show Gist options
  • Select an option

  • Save aprell/ed1df6f758252c79b361fbc048e7fb9e to your computer and use it in GitHub Desktop.

Select an option

Save aprell/ed1df6f758252c79b361fbc048e7fb9e to your computer and use it in GitHub Desktop.
Convenience script for compiling and running C/C++ programs
#!/usr/bin/env bash
# ShellChecked with no issues detected
set -eu
set -o pipefail
compile() {
local args
args=$(head -1 "$src" | sed 's/^\/*//')
# Ignore any Makefiles that might be present
eval "make -f /dev/null $args $exe"
}
run() {
./"$exe" "$@"
}
src="$1"
exe="${src%.*}"
shift
compile && run "$@"
// CC=clang CFLAGS="-Wall -Wextra"
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
@aprell

aprell commented Aug 25, 2019

Copy link
Copy Markdown
Author
$ run.sh test.c
clang -Wall -Wextra test.c -o test
argv[0] = ./test

$ run.sh test.c 1 two "three four" 5
make: `test' is up to date.
argv[0] = ./test
argv[1] = 1
argv[2] = two
argv[3] = three four
argv[4] = 5

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