6 ms·
Want to "skip" the compile step? //usr/bin/tcc -run $0; exit main() { printf("Hello\n"); return 0; } Just `chmod +x` and run it...
by jonathonf 6y ago
Want to "skip" the compile step?
//usr/bin/tcc -run $0; exit
main() { printf("Hello\n"); return 0; }
Just `chmod +x` and run it...
- shakna 6y agoAnd add the -b commandline flag to enable the memory and bounds checker.
- mmastrac 6y agoHow does this work? Is there an alternate form for #! (aka shebang)? EDIT: hah, I should have realized that "//" is the C comment and "//usr/bin/tcc" is equivalent to "/usr/bin/tcc". Clever!
- dfabulich 6y ago"C comment," you mean. So it's running the file as a shell script, where the first line runs tcc on the current file and quits.
- mmastrac 6y agoFixed typo, thanks. Yeah for some reason it didn't click when I saw it.
- banana_giraffe 6y agoI always love these things that are really scripts in two (or more) languages. Common is something like this on Windows to make a command script a Perl script. It lets you run your Perl script without having to make sure you had .pl extensions associated. Though, you needed Perl installed, so I never quite got the point. @rem = '--*--Perl-*-- @echo off echo Hello from a command script perl -x -S %0 %* goto endofperl @rem '; #!perl print "Hello from Perl!\n"; __END__ :endofperl By far very less common is to stuff some Perl code in a C# source file. A dev that was way too clever for his own code did this to have a Perl script that would update a C# script file and be self contained. This is a simple example of what it looked like, minus the instant headache anyone got that had to look at the real version: #if PerlScript $csharp=<<WhyJustWhy; #endif using System; namespace Example { public static class Program { static void Main() { Console.WriteLine("Hello from C#"); } } } #if PerlScript WhyJustWhy print("Hello from Perl!") __END__ #endif
- hawski 6y agoIt's obviously not the same, but you can emulate this with a conventional compiler: #if 0 set -e; [ "$0" -nt "$0.bin" ] && cc "$0" -o "$0.bin" exec "$0.bin" "$@" #endif #include <stdio.h> int main(int argc, char *argv[]) { puts("Hello world!"); return 0; } It works, because by default system shell will be spawned.
- sergeykish 6y agoFrom man tcc(1): #!/usr/bin/tcc -run #include <stdio.h> int main() { printf("Hello World\n"); return 0; }
- keyle 6y agoJust as a side note... It's important to say that it still does get compiled... it's not some kind of interpreted mode.
- nialv7 6y agoD code can also be run this way: #!/usr/bin/rdmd void main() { import std.stdio; writeln("hello"); }