Hacker News new | ask | show | jobs
by jonathonf 2180 days ago
I was just about to reply about the same thing:

  #!/usr/bin/tcc -run
  
  #include <stdio.h>
  
  int main() {
    printf("Hello\n");
    return 0;
  }
1 comments

Or even

  tcc -w -run - <<< 'main() { printf("Hello\n"); return 0; }'           
However, runc seems more convenient to pass in just the statements.
That wasn't quite what I was trying to show with the snippet - I was intimating that you can use tcc to run C directly in something like a shell script.

If you want to make `tcc -w -run` etc. more convenient then:

  alias trun='tcc -w -run - <<<'
  trun 'main() { printf("Hello\n"); return 0; }'
---

I can't edit my parent comment now, but it can be improved with e.g.

  //usr/bin/tcc -run $0; exit
  main() { printf("Hello\n"); return 0; }
> That wasn't quite what I was trying to show with the snippet - I was intimating that you can use tcc to run C directly in something like a shell script.

I know, but the first example in the link was:

  $ runc 'printf("%s\n", "Hello!");'  
  Hello!
So I figured I'd complement your example use with one that was more analogous to that.

> I can't edit my parent comment now, but it can be improved with e.g.

To be honest, I liked your original example more. Using that `//` "shebang" depends on what shell it's invoked from, and it wouldn't work when calling exec from other languages. For example, with your last example using `//`:

  $ python -c 'import subprocess; subprocess.run(["./hello-tcc-double-slash-shebang.c"])'   
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/usr/lib/python3.8/subprocess.py", line 489, in run
      with Popen(*popenargs, **kwargs) as process:
    File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
      self._execute_child(args, executable, preexec_fn, close_fds,
    File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
      raise child_exception_type(errno_num, err_msg, err_filename)
  OSError: [Errno 8] Exec format error: './hello-tcc-double-slash-shebang.c'
while it would work with the `#!` shebang, since that's handled by the kernel.