Skip to content

Instantly share code, notes, and snippets.

@hadrianw
Last active November 13, 2022 21:30
Show Gist options
  • Save hadrianw/5cd275d5838814d813adc12978dce9be to your computer and use it in GitHub Desktop.
Save hadrianw/5cd275d5838814d813adc12978dce9be to your computer and use it in GitHub Desktop.
Make a C file executable with those few lines.
#if 0
set -e; [ "$0" -nt "$0.bin" ] &&
gcc -Wall -Wextra -pedantic -std=c99 "$0" -o "$0.bin"
exec "$0.bin" "$@"
#endif
#include <stdio.h>
int
main(int argc, char *argv[]) {
int ch = getchar();
printf("Hello %c world!\n", ch);
return 0;
}
@stefanos82
Copy link

Yeah, it makes sense as you describe the whole behavior.

Basically I thought of TCC's and Dlang's -run flags; what I know for sure is that DMD would generate an executable at /tmp, run it, and then delete it immediately.

Of course as you have said yourself, the workarounds you have just shared (thank you by the way) are quite hacky and a bit verbose to say the least, therefore I should stick with your original preprocessor technique.

Thank you for your thorough explanation, I was reminded how script behavior works.

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