Ubuntu – have to name the text file .cpp in order to compile a C++ program from command line

file format

I'm exploring compiling/running simple C++ programs from the bash shell (in Ubuntu 12.04). Being new to Linux in general, I actually have a couple questions.

First, it seems that Ubuntu/Linux does not display file types in the .abc format, as Windows does. I get that the OS doesn't need that extension to know the file's type, but isn't it more convenient for the human user to be able to read off that information at a glance? How am I supposed to know what type a given file is?

Second (and relatedly), if Linux seems not to care about extending a file name with the type information, why is that when I create a simple C++ program in gedit and just call it 'test', the compilation doesn't work (using the g++ compiler btw), but when I name the file 'test.cpp', it does work?

Does Linux need/care about the extension, or not? I feel like I'm getting mixed messages here, and I would really like to understand.

Best Answer

You don't have to name your file with a particular suffix, however the gcc/g++ compilers use the suffix as a shortcut to decide how to process the files. If you want to use a different suffix (or no suffix) you can tell the compiler what kind of file it is explicitly using the -x command line option - see the answer to this similar previous question Compiling C source file without .c suffic