There should be no need to put header files in the list of files to be compiled, since a stylistically correct header file generates no executable code.
It used to be the case that it was mostly harmless to put header files in the gcc command line, because the compiler would add no executable content to the output file as a result of parsing and compiling a header. However, since gcc version 4 or so, and for roughly the same amount of time for clang, header files on the command-line are compiled into precompiled headers for use in later compile steps.
That means that compiling
gcc x.c y.h
will create two products: an executable generated from x.c
and a precompiled header generated from y.h
.
Gcc (at least up to version 6.3) lets you specify an explicit output filename in this case, although I believe the consequence is that the precompiled header file is written as x
and then overwritten by the executable. (It doesn't let you specify an explicit output name in most other cases, such as when you use the -c
, -S
or -E
options to produce an output for every input.) But clang, possibly more sensibly, produces an error when you an explicit output filename with the -o
option and you have more than one output, even when that output is a precompiled header (which you possibly didn't intend to produce).
(Confusingly, on Mac OS X, the command gcc
normally invokes the clang compiler. I suppose this is to avoid breaking scripts which incorrectly believe that gcc
is the generic name for a C compiler.)
The solution is to remove the header files from the list of files to be compiled.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…