cpp or cc extension?

J

Johs

I have read various tutorials on the net on c++ and sometimes they use
..cc as extension to files and other times they use .cpp or even just .c.
Are there no strict rules for naming of sourcefiles?
 
Z

Zeppe

Johs said:
I have read various tutorials on the net on c++ and sometimes they use
.cc as extension to files and other times they use .cpp or even just .c.
Are there no strict rules for naming of sourcefiles?

There are no strict rules AFAIK, but there are de-facto standard. That is:
- c source files: c
- c header files: h
- c++ source files: cpp, cc, cxx
- c++ header files: h, hh, hpp

at least for the most common. I won't use the c extension for a cpp
program, because many compilers that compile both c and c++ interpret
the file as a c source if it has the c extension. For the header files
there is any problem indeed because the keep the context of the
translation unit in which they are included.

Regards,

Zeppe
 
V

Victor Bazarov

Zeppe said:
There are no strict rules AFAIK, but there are de-facto standard.
That is:
- c source files: c
- c header files: h
- c++ source files: cpp, cc, cxx

In the early days (and maybe just to be difficult^H^H^H^H^Herent),
Unix used .C (capital C) extension for C++ files. Since MS operating
systems have case insensitive file names, copying .C files from the
Web to a Windows-run system loses the distinction between .c and .C
files. If you know that it's a C++ source, rename it to .cpp right
away.
- c++ header files: h, hh, hpp

at least for the most common. I won't use the c extension for a cpp
program, because many compilers that compile both c and c++ interpret
the file as a c source if it has the c extension. For the header files
there is any problem indeed because the keep the context of the
translation unit in which they are included.

Case insensitivity is a bliss, ain't it?

V
 
J

James Kanze

There are no strict rules AFAIK, but there are de-facto standard. That is:
- c source files: c
- c header files: h
- c++ source files: cpp, cc, cxx
- c++ header files: h, hh, hpp
at least for the most common. I won't use the c extension for a cpp
program, because many compilers that compile both c and c++ interpret
the file as a c source if it has the c extension. For the header files
there is any problem indeed because the keep the context of the
translation unit in which they are included.

Historically, the original suffix for C++ was .C. Which, of
course, poses problems on systems which don't distinguish case
in filenames (most of them, I think---at least, I've never
encountered any but Unix which do distinguish case). For
whatever reasons, the convention seems to have become
established of using .cpp in the Windows world, whereas .cc
seems more frequent (but not exclusive) under Unix.

All of the compilers I know will treat either as a C++ source in
their most recent incarnations, and all have always had an
option to force treating the source as C++. Just be consistent,
and don't worry about it.

The conventions for header files are even vaguer. The original
convention was just to use .h, and not distinguish them from C
header files. Apparently, I wasn't the only one who didn't like
this, and a lot of people adopted different rules: .H, .hpp or
..hh. Again, just be consistent (although I do think it better
to restrict .h to headers which can be used within a C program
as well).
 
Z

Zeppe

James said:
The conventions for header files are even vaguer. The original
convention was just to use .h, and not distinguish them from C
header files. Apparently, I wasn't the only one who didn't like
this, and a lot of people adopted different rules: .H, .hpp or
.hh. Again, just be consistent (although I do think it better
to restrict .h to headers which can be used within a C program
as well).

Just to add a final note on this, some graphical editors can decide to
highlight .h files as "c source" and .hpp files as "c++ source".

Bye!

Zeppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top