iammilind
iammilind

Reputation: 69988

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. e.g. One functionality difference I see is that .cxx files can have their likable object files.

Upvotes: 1

Views: 222

Answers (3)

T33C
T33C

Reputation: 4429

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.

Upvotes: 1

Jason
Jason

Reputation: 32510

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.

Upvotes: 5

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234444

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;

Upvotes: 5

Related Questions