Binfmtc – binfmt_misc C scripting interface

39 todsacerdoti 10 6/8/2025, 12:38:55 PM netfort.gr.jp ↗

Comments (10)

rwmj · 1h ago
You can already do this without using binfmt ...

  #if 0
  gcc "$0" -o "$@".out && exec ./"$@".out
  #endif
  #include <stdio.h>
  int main () { printf ("hello, world\n"); return 0; }
Usage:

  $ chmod +x print.c
  $ ./print.c
  hello, world
(Could be better to use a temporary file though.)

There's a similar cute trick for compiled OCaml scripts that we use with nbdkit: https://libguestfs.org/nbdkit-cc-plugin.3.html#Using-this-pl...

mananaysiempre · 27m ago
Compiler errors won’t cause as many funny consequences with

  gcc "$0" -o "$@".out && exec ./"$@".out || exit $?   # I'd use ${0%.c} not $@
Love this trick too, but the difference, as far as I understand, is that it only works with a Bourne(-compatible) shell, whereas shebangs and/or binfmt_misc also work with exec().
radiospiel · 1h ago
Amazing; when I tried something similar I used a "#!" line pointing to a C compiler + runner of sorts (https://github.com/radiospiel/jit). https://git.zx2c4.com/cscript/about/ is also following that approach.

What is the benefit of registering an extension via binfmt_misc?

lmz · 1h ago
It's still valid C source code? Is the #! sequence valid C?
zx2c4 · 3h ago
Similar project of mine from a long while ago: https://git.zx2c4.com/cscript/about/
monocasa · 50m ago
There's also tcc, which has a shebang compatible extension to allow it to be used by scripts.
enriquto · 1h ago
was surprised that "sudo apt install binfmtc" works out of the box on my box (linux mint) and i can do the magic just as described here
elitepleb · 3h ago
JSR_FDED · 2h ago
C is still my first love. You can hold the whole language in your head, and it’s fast. Yes there are footguns but it’s a libertarian programming language - you’re responsible for what you build. No hand holding.

No comments yet

Surac · 3h ago
So lovely