编译不带 .c 后缀的 C 源文件

cou*_*que 7 gcc

让 hello_world 成为有效的 C 源文件。当我尝试编译时,我得到

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

我知道我可以重命名为 .c 后缀,但如果我不这样做,gcc 是否支持编译没有 .c 后缀的文件?

ste*_*ver 11

您可以使用-x命令行选项。从 gcc 手册页:

   -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
Run Code Online (Sandbox Code Playgroud)

例如

$ gcc -Wall -o hello cfile
cfile: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

$ gcc -Wall -o hello -x c cfile
$ ./hello
Hello world!
Run Code Online (Sandbox Code Playgroud)