Bug 41492 - Please ignore #! on the first line of a file
Summary: Please ignore #! on the first line of a file
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-28 19:53 UTC by D W
Modified: 2019-08-11 06:14 UTC (History)
1 user (show)

See Also:
Host: All
Target: All
Build: All
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description D W 2009-09-28 19:53:16 UTC
I have a file that I would like to be able to either execute with a C interpreter, or compile with GCC.
As you know, Unix uses "#!/path/to/interpreter" to invoke scripts.  Consider the following program:

#!/usr/bin/c-interpreter
int main() { printf("Hello World\n"); }

It would be nice if this could be compiled by GCC, simple by ignoring the first line of the file.  The current error message is:
hello.c:1:2: error: invalid preprocessing directive #!

One easy fix would be to define the preprocessing directive #! as a legal directive, which swallows the rest of the line.  This would work with C, C++, and ObjC, but not the other languages.  Another possibility that would work universally is to make #! as the first two characters of an input file a "special case" comment specifier in all front-ends.  It doesn't need to be recognized anywhere except as the first two bytes of a file.

Please consider this small but useful feature as it will help me with writing portable code that can be interpreted as well as compiled.
Comment 1 Joseph S. Myers 2009-11-22 19:31:49 UTC
I don't think this is an appropriate feature; I advice arranging for your
source files to go through an external preprocessor before compiling them
if you need this special sequence at the start of the file.
Comment 2 felix 2019-08-11 06:14:28 UTC
Please reconsider. For one thing, this extension does not in any way interfere with normal preprocessor syntax. For another, stripping the shebang line in an external preprocessor introduces overhead and complications: one has to create a temporary source file, probably insert a #line directive in the shebang line's place, and arrange for the temporary file to be deleted when compilation finishes. Being able to have the compiler read the file directly would make this so much simpler.

Also, I sometimes develop small C programs by alternating between rapid write-compile-run cycle with Tiny C Compiler and running the program though a normal compiler to look for warnings and produce an optimised binary. Right now, if I do this, I have to comment out or remove the shebang line when compiling, and add it back when I want to invoke the source file directly from the command line through TCC. This hardly stops me dead in my tracks, but is sure irritating.

Plus, the compiler can easily keep generating a warning for erroneous shebang lines (or for all shebang lines when -Wpedantic is in force), so the likelihood of this feature leading to accepting erroneous code is minimal.