https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100392
Bug ID: 100392 Summary: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF Product: gcc Version: 11.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: preprocessor Assignee: unassigned at gcc dot gnu.org Reporter: mst.lo at arcor dot de Target Milestone: --- > g++ --version g++ (GCC) 11.1.1 20210428 (Red Hat 11.1.1-1) works: > echo '#include "test.h"' > test.cpp > printf 'extern const int ksc5601_hangul_to_unicode[2350];\n' > test.h > g++ -E -fdirectives-only test.cpp -o test.i > g++ -c test.i doesn't work: > echo '#include "test.h"' > test.cpp > printf 'extern const int ksc5601_hangul_to_unicode[2350];' > test.h > g++ -E -fdirectives-only test.cpp -o test.i > g++ -c test.i In file included from test.cpp:1: test.h:1:50: error: stray ‘#’ in program 1 | extern const int ksc5601_hangul_to_unicode[2350]; | ^ test.h:1:52: error: expected unqualified-id before numeric constant 1 | extern const int ksc5601_hangul_to_unicode[2350]; | in a draft of C++11, 2.2 Phases of translation [lex.phases] 1.2 says: A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file. similar language in https://github.com/cplusplus/draft/blob/master/source/lex.tex#L97 wheres e.g. in a draft of C99, 5.1.1.2 Translation phases 2. says: A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place. ... so apparently different standards have different requirements here. i believe this problem didn't occur when using GCC 10 with sccache or icecream, which use "-E -fdirectives-only" to create a suitable input for distributed compilation.