5

Lot of the times when I watch other people's code I see some are including a .h file and some are including a .c/.cpp file. What is the difference?

6
  • 4
    Technically nothing, you can include any filename you like, and the file is just opened and copied in place. Typically, .h files hold the declarations of functions, etc, and .cpp files hold definitions (which you don't need to see to call the function). It is rather odd to #include a .cpp file. Commented Aug 25, 2014 at 9:35
  • 2
    The main difference is, that including .c/.cpp files almost always leads to serious trouble. Commented Aug 25, 2014 at 9:37
  • What kind of trouble? Commented Aug 25, 2014 at 9:42
  • 1
    @eddard.stark "Multiple definition" errors during linking phase in most cases Commented Aug 25, 2014 at 9:43
  • 1
    @eddard.stark See here please (and the question accordingly). Commented Aug 25, 2014 at 9:47

4 Answers 4

7

It depends on what is in the file(s).

The #include preprocessor directive simply inserts the referenced file at that point in the original file.

So what the actual compiler stage (which runs after the preprocessor) sees is the result of all that inserting.

Header files are generally designed and intended to be used via #include. Source files are not, but it sometimes makes sense. For instance when you have a C file containing just a definition and an initializer:

const uint8_t image[] = { 128, 128, 0, 0, 0, 0, ... lots more ... }; 

Then it makes sense to make this available to some piece of code by using #include. It's a C file since it actually defines (not just declares) a variable. Perhaps it's kept in its own file since the image is converted into C source from some other (image) format used for editing.

Sign up to request clarification or add additional context in comments.

3 Comments

So what should I put in the .h file and what should I put in the .c file?
Should I put the function prototypes in the .h file and the function in the .c file?
@eddard.stark Yes, that's very common. Headers are used to describe the public interface to a module of code implemented by a C file. Do not include any static function prototypes, they're not public.
2

.h files are called header files, they should not contain any code (unless it happens to contain information about a C++ templated object). They typically contain function prototypes, typedefs, #define statements that are used by the source files that include them. .c files are the source files. They typically contain the source code implementation of the functions that were prototyped in the appropriate header file.

Source- http://cboard.cprogramming.com/c-programming/60805-difference-between-h-c-files.html

Comments

1

you can look at gcc website (https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html) that reports a good summary of all the extensions that you can use in C/C++:

C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

Comments

1

Including header file with declarations is the main, recommended and used almost anywhere, method for making consistent declarations among a project. Including another source file is another (very rare) kind of beast, and it's useful and possible under specific conditions:

  1. There is a reason to split code to separate source files despite it shall be compiled as a single module. For example, there are different versions of some functions which shan't be visible from another modules. So, they are declared static but which version is included is regulated by compile options. Another variant is size and/or maintanenance credentials issues.
  2. The included file isn't compiled by itself as a project module. So, its exported definitions aren't in conflict with the module that file is included to.

Here, I used terms definition and declaration in the manner that the following are declarations:

extern int qq; void f(int); #define MYDATATYPE double 

and the following are definitions:

int qq; // here, the variable is allocated and exported void f(int x) { printf("%d\n", x); } // the same for function 

(Also, declarations include C++ methods with bodies declared inside their class definition.)

Anyway, the case another .c/.cxx/etc. file is included into source file are very confusing and shall be avoided until a very real need. Sometimes a specific suffix (e.g. .tpl) is used for such files, to avoid reader's confusion.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.