[an error occurred while processing this directive] Node:Which language, Next:, Previous:C++ comments, Up:Compiling

8.5 How does GCC recognize the source language?

Q: I type GCC PROG.CC and GCC complains that it can't recognize PROG.CC's file format. How come a C++ compiler doesn't recognize a C++ source??

Q: I type GCC PROG.C to compile a C program which I already remember to pass compilation without a single warning, and suddenly it gives all kinds of strange error messages and unresolved externals.

A: That's because you typed your source file extension in UPPER case. GCC is not case-insensitive about filenames like DOS is, and it uses the file's extension to determine how to compile a file. Valid extensions are:

.cc
.C
.cxx
.cpp
C++ source (passed through cpp).
.c
C source that must be passed through cpp first.
.i
Raw C source (no cpp pass).
.ii
Raw C++ source (not to be preprocessed).
.m
Objective-C source.
.S
Assembler that must be passed through cpp first.
.s
Raw assembler source (no cpp pass).

Any other file is passed to the linker, under the assumption that it's an object file.

In the examples above, PROG.C is taken as a C++ program, not a C one, and PROG.CC is passed to the linker as if it were an object file. You can see what GCC does by adding the -v switch to the GCC command line; if you see that it's invoking cc1plus.exe (the C++ compiler) instead of cc1.exe (the C compiler), or calling ld.exe (the linker) on a source file, then you'd know this is your problem. If you have problems keeping up with the verbose GCC output triggered by -v, see how to capture GCC output, earlier in this FAQ.

You can override the default rules gcc uses to decide how each input file should be treated, using the -x language switch. For instance, the command

 gcc -x c++ prog.c

compiles prog.c as C++ source. See The GNU C Compiler Manual, for more info on -x options.


[an error occurred while processing this directive]