3 ms·
Rather than using shebang notation for running C source files, which as noted leads to code which isn't legal input to a normal C compiler, there is a standard
by psykotic 10y ago
Rather than using shebang notation for running C source files, which as noted leads to code which isn't legal input to a normal C compiler, there is a standard trick to please both the shell and the C compiler:
#if 0
/bin/env cpi "$0"
exit
#endif
And you don't need this cpi program depending on your tolerance for hacks:
#if 0
out=/tmp/$RANDOM
gcc "$0" -o $out
$out
rm $out
exit
#endif
- jjnoakes 10y agoThis only works if invoked by the shell, I believe. If you exec that file directly it will fail unless I'm mistaken (can't test at the moment).
- psykotic 10y agoYeah, I should have mentioned that. Whereas shebang notation is understood by the executable loader, not the shell.
- RhysU 10y agoUse exec. Save an exit.