Rather than expecting all C compilers to be modified to
ignore the #! line, it should be possible to configure
/bin/sh to do the desired thing. If a file is
executable and has no #! line, the kernel will execute
it via /bin/sh. Anyone who wants this facility could
create a .profile file (or similar) that checks for a
file argument, and checks if it is C source, and then
arranges for it to be compiled and executed and clean
up afterwards (or not). I don't think /bin/sh or any C
compilers would need to be modified. The actual dotfile
is left as an exercise for the reader. :-)

cheers,
raf

On Sun, Dec 29, 2024 at 01:08:48PM -0600, Paul Markfort via Gcc 
<gcc@gcc.gnu.org> wrote:

> 
> You can also do what I do now (the example in my first message), and don't 
> need to pre-process the file before sending it to the compiler.  What 
> Jonothan suggested ("Still it would be a nice touch ...") would be great - 
> but simply being able use a custom script to (like I do in the example) 
> without all the convolutions I used to make it work - would be a good 
> solution.
> 
> My goal would just be to have C, C++, etc, simply ignore the "#!" line, if it 
> is the very first line.
> If you want to have additional lines, have it ignore everything until it sees 
> an  "#end" (or some other token).
> 
> Basically "#!" would become a special comment (but would only work if the 
> file essentially started with it - first non-blank characters).
> 
> Think of users who use scripts all the time, but rarely use Compiled 
> languages.
> 
> 
> 
> 
> 
> On 2024-12-28 4:47 PM, Florian Weimer wrote:
> > * Jonathan Wakely via Gcc:
> > 
> > > Here's a complete example:
> > > 
> > > #!/bin/sh
> > > set -e
> > > out=$(mktemp /tmp/a.out.XXXXXX)
> > > sed 1,5d "$0" | gcc -x c - -o "$out"
> > > exec "$out"
> > > 
> > > #include <stdio.h>
> > > int main()
> > > {
> > >    puts("Hello, world");
> > >    return 0;
> > > }
> > 
> > Or this, with accurate locations for diagnostics and argument
> > handling:
> > 
> > #!/bin/sh
> > set -e
> > out=$(mktemp /tmp/a.out.XXXXXX)
> > (echo "#line  6 \"$0\""; sed 1,5d "$0") | gcc -x c - -o "$out"
> > exec "$out" "$@"
> > 
> > #include <stdio.h>
> > int main()
> > {
> >    puts("Hello, world");
> >    return 0;
> > }
> > 
> > Still it would be a nice touch if we could do
> > 
> > #!/usr/bin/gcc -f
> > #include <stdio.h>
> > int main()
> > {
> >    puts("Hello, world");
> >    return 0;
> > }
> > 
> > instead.
> > 
> 
> -- 
> --------------------------------------------------------
> The views and opinions expressed above are strictly
> those of the author(s).  The content of this message has
> not been reviewed nor approved by any entity whatsoever.
> --------------------------------------------------------
> Paul FM         Info: http://paulfm.com/~paulfm/
> --------------------------------------------------------

Reply via email to