Compiling C source file without .c suffix

Let hello_world be a valid C source file. When I try to compile I get

gcc hello_world // hello_world: file not recognized: File format not recognized

I know I could rename to have .c suffix, but if I don't, does gcc support compiling file without .c suffix?


You can use the -x command line option. From the gcc man pages:

   -x language
       Specify explicitly the language for the following input files
       (rather than letting the compiler choose a default based on the
       file name suffix).  This option applies to all following input
       files until the next -x option.  Possible values for language are:

               c  c-header  cpp-output
               c++  c++-header  c++-cpp-output
               objective-c  objective-c-header  objective-c-cpp-output
               objective-c++ objective-c++-header objective-c++-cpp-output
               assembler  assembler-with-cpp
               ada
               f77  f77-cpp-input f95  f95-cpp-input
               go
               java

For example

$ gcc -Wall -o hello cfile
cfile: file not recognized: File format not recognized
collect2: ld returned 1 exit status

but

$ gcc -Wall -o hello -x c cfile
$ ./hello
Hello world!