Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is doable entirely without a Linux-specific binfmt-misc hack.

https://rosettacode.org/wiki/Multiline_shebang#C




This is a neat hack, but the whole file is not a valid C program.


You mean it consists of a C program, plus non-C cruft to get it running?

Isn't that already legitimized by configure scripts, compiler command lines and Make files?


of course it is legitimized. I just meant that the selling point of binfmtc is that the same .c file can either be compiled by any C compiler, or executed directly.


We could perhaps split it into a two-file system where a "foo.sh" containing certain boilerplate will execute a "foo.c" that doesn't require any special conventions at all.

"foo.sh" could be identical for any .c file, and so we could symbolically link them all to a common file.

Of course, neither that file nor symlinks to it require a .sh suffix.

The contents might look like this:

  #!/bin/sh
  c_file=${0%.sh}.c
  x_file=${0%.sh}.bin

  # If cached executable file is missing, or out of date w.r.t.
  # the source file, compile it:

  if [ "$c_file" -nt "$x_file" ]; then
    cc $CFLAGS "$c_file" -o "$x_file"
  fi

  # Run cached executable file
  $x_file




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: