Does it matter which file extension I use for my C++ programs?

I am using Orwell Dev C++ 5.8.3 IDE. It uses g++ 4.8.1 compiler. When I am saving my C++ program files it shows to several file extensions for saving my C++ program files like .cpp, .cc, .cxx, .c++, .cp .

Does it matter which extension I choose?

Are these extensions compiler/IDE specific or platform specific?

I am using windows 7 operating system.

Are these extensions also available and / or valid to use on other operating systems like Unix or Linux?

Why there are so many extensions provided?

What is the reason?


The standard does not mandate any format for C++ source files, it even allows a compiler to get the source from a .jpeg image of handwritten code on a piece of paper and compile that. (Funny read)

However, such a compiler does not exist (of course). Most real world tools would work best with any of the conventional file endings like .cpp , .cc or .cxx , so I would suggest not to go completely ham with your endings. Any of the conventional ones are equally good, just stay consistent in your project to avoid confusion.

I also would be a little bit careful with stuff like .c++ . I do not know whether every system out there allows such things in file names.


Does it matter which extension I choose?

Yes and no. No, it doesn't matter what extension you use. You could use ".bat" or ".frog", as long as you tell the compiler what language you are compiling.

No, you stay away from extensions that have different meanings, such as ".com" and ".exe".

The extensions you listed are common throughout the industry.

Are these extensions compiler/IDE specific or platform specific?

No, they are not exactly specific to a platform but more common on different platforms.

I try to get my development team to use ".hpp" instead of ".h" since we are mixing C and C++ language files; but they don't understand and continue to use '.h' for both languages.

Are these extensions also available and / or valid to use on other operating systems like Unix or Linux?

The language extensions are independent of the operating system. You can use ".frog" on a Windows or Linux OS. Make sure the OS allows for the length of the extension.

Why there are so many extensions provided?

Creativity, stubbornness, refusual to adapt to a standard.

The biggest problem was that operating systems could not handle "++" in their filenames or extensions. The C language extension was fine, but some OS were case sensitive. When the C++ language came along, many OS could not support an extension of ".c++" because of the "++" characters. So they came up with different varieties:

cpp - C plus plus
cxx
cc
cp

Another issue early in the days is that the C++ source files would be preprocessed and then fed to a C compiler. So the extension for preprocessed files was ".cpp", which added more to the confusion.

What is the reason?

Lazy programmers, programmers with habits, grabbing market share.

A compiler manufacturer wants to sell their compiler product to the widest possible audience. So they will adapt.

When a programmer is used to the extension ".cxx" and is forced to use another manufacturer's compiler that only uses ".cc", the programmer will not use the compiler or slow down productivity (by keep having to change the extension), out of habit.

Thus, to please the widest audience, the compiler manufacturers have a table or map of common extensions and languages. If you feed a ".c" file to the GNU compiler, it will assume the C language. Likewise, a ".cxx" file will be assumed to have C++ code. Again, these are common extensions.

You can tell the compiler to compile a file using a different language. For example, you can tell the compiler to translate a ".c" file as ".c++". This is a common method of sharing C files with the C++ language.

链接地址: http://www.djcxy.com/p/86308.html

上一篇: std :: string在.c文件中

下一篇: 这与我用于C ++程序的文件扩展名有关系吗?