Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .h files and .inc files in c

I have been looking at various open source projects, and they have a bunch of files written in C that have .inc as a file extension. As far as I can tell, they are used like header files.

I thought the standard convention was to use .h files for header files and .c for source files. So, is there a standard convention on when a header file should be an .inc file rather than being a .h file, or are these decided only at a per project basis? (Or am I just looking at weird projects that use .inc?)

like image 338
Leif Andersen Avatar asked Jul 15 '16 18:07

Leif Andersen


People also ask

What is .INC file in C?

An INC file is an include file that is used by software program's source code to reference headers information. It is similar to the . h file format and contains declarations, functions, headers, or macros that can be reused by source code files such as C++.

What is the difference between .h and .c files in C?

c files contain the implementation of the code, while . h files exist to provide interfaces that allow a file to access functions, global variables, and macros from other files.

What is the difference between .h and .HPP files?

You should use the . hpp extension if you're working with C++ and you should use . h for C or mixing C and C++.

What is .h file and .cpp file?

h file contains shared declarations, a . cpp file contains definitions and local declarations. It's important that you understand the difference between declarations and definitions.


2 Answers

The standard convention is to use .h for header files that contain only macro definitions and other declarations and .c for C source files that contain code and data definitions.

In some projects, code fragments are stored in separate files and included in C source files with some macro tricks to expand specifically in different files or circumstances. Sometimes, these files are even included multiple times in the same source file. To underscore the special semantics attached to such files, giving them a different extension may be a useful hint. These files may also have been generated as part of the build process: naming them specifically may prevent confusion with actual source files.

Look at the files you came across in these projects and verify if this explanation holds.

like image 181
chqrlie Avatar answered Oct 05 '22 23:10

chqrlie


.inc files are often associated with templated classes and functions.

Standard classes and functions are declared with a .h file and then defined with a .cpp file. However, a template is neither a class nor a function but a pattern that is used to generate a family of classes or functions. In order for the compiler to generate the code for the template, it needs to see both the declaration and definition and therefore they both must be included in the .h file.

To keep the declaration and definition separate, the definition is placed in its own file and included at the end of the .h file. This file will have one of many possible file extensions .inc, .imp, .impl, .tpp, etc.

Declaration example:

// Foo.h
#ifndef FOO_H
#define FOO_H

template<typename T>
class Foo {
public:
    Foo();
    void DoSomething(T x);
private:
    T x;
};

#include "Foo.inc"
#endif // FOO_H

Definition example:

// Foo.inc
#include "Foo.h"

template<typename T>
Foo<T>::Foo() {
    // ...
}

template<typename T>
void Foo<T>::DoSomething(T x) {
    // ...
}
like image 29
AllDay Avatar answered Oct 05 '22 23:10

AllDay