简体   繁体   中英

What difference it will make, if we have uniform extension (.c/.cpp) for all C/C++ files?

In C/C++ project, mostly the file can be of either types .h or .c / .cpp . Apart from their naming difference such as header and implementation files; is there any functionality difference ?

In other words: if in a working C/C++ project what difference it makes if we change all files with .c or .cpp extension ?

[Note: We can also have #include guards for .c/.cpp files. We can skip their compilation if they are observed as headers.]

Edit : Debate is not intended for this as I don't have any solid use case. Rather I wanted to know, that allowing to give .h , .hxx , .i extensions are just a facility or a rule. eg One functionality difference I see is that .cxx files can have their likable object files.

What difference does it make? The compiler is perfectly happy about it. To it, it's just files.

But to you? You makes a lot of difference:

  • you're no longer able to immediately figure out which one is the header and which one is the implementation;
  • you can no longer give header and implementation the same name;

If you are using gcc, and you try and compile a bunch of C++ files labled with a .c extension, it's going to try and compile your file as-if it were a C-language file, which is going to create a ton of errors.

There's also project sanity as well ... this is why many times you'll see projects actually label C++ headers as .hpp rather than just .h so that it's easier to create a distinction between C-language source-code and headers, and C++ source-code and headers.

Header files generally must not be compiled directly but instead #included in a file that is directly compiled. By giving these two groups of files their own extension it makes it a lot easier to determine which to compile.

Make and IDE's and other tools normally expect the conventions of .c/.cpp for source and h/hpp for header. Compiler normally goes a step further and defaults to C compilation for .c and c++ compilation for .cpp

Hence, a bad idea to give your headers the same extension as the the source files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM