what is the different between all the include types ?

hello all
im confuse here there is several include types and i dont understand why for example
in the c++ file i can see :

<somefile>
or
<somefile.h>
or
"somefile.h"

what is the difference between them ?

also what is the difference if for example i but the include file im using in the header file and then i include the header file
in the c++ file or just put the include files in the cpp file ?

thanks
The difference is <somefile.h> is a file whose location is known by the compiler. "somefile.h" is a local file to the directory you're compiling in, which you probably wrote. <somefile> is a variant of <somefile.h>.

The purpose of including a library in your *.cpp file is so you can re-use the functions of the library. Include libraries in the files you write that they directly apply to. In other words, unless you have code in your *.h file that uses functions in that library, you should include it in the *.cpp. Generally the declarations of functions are put in header files (.h), and they are defined in a separate .cpp file that includes that file and all necessary libraries.

That said, if you include the library in your .h file, and you include the .h file in your .cpp file, the .cpp with have the library's functions. The problem with that is the compiler will flip if you have two functions with the same name and argument types, which can happen if it goes over the same file more than once (i.e. you have nested files that include it). But then again, you can avoid that by organising your header files like so:

1
2
3
4
#ifndef HEADERFILENAMEINCAPS_H
#define HEADERFILENAMEINCAPS_H
/* all of your usual code */
#endif 


That's a preprocessor directive that checks if a constant hasn't been defined. If not, the compiler continues. You then define that constant so that if the compiler attempts to compile it again, it skips the code, and doesn't have multiples of functions. The constant doesn't need a value. It's common practice to use the name of the file as the constant's name, and as a constant it is generally capitalised.
Last edited on
In general, you should try to have as few include directives as possible in header files. This makes the code faster to compile, and makes the interface more "independant" from the implementation.

To make sure your header file contains all necassary includes, make sure you always include your local header files *before* any external libraries in the cpp file. The very first header file included in "yourfile.cpp" should always be "yourfile.h" (if it exists).

There's one common trick you might use to reduce the number of include directives you need in the header file, called "forward declaration". It it very common for a class to have a pointer or reference to another class as a member variable, which is declared in the header file, but only accessed in the cpp file. In this case, the compiler only need to know that the class exists when compiling your header file. It doesn't need the full specification. It's possible to only "forward declare" the class in your header file, and then include the header file in your cpp file:

1
2
3
4
5
6
7
8
// yourclass.h
class LibraryCass; // Forward declaration

class YourClass {
  LibraryClass* cl;
public:
  YourClass(LibraryClass* c);
};


1
2
3
4
5
6
7
8
// yourclass.cpp
#include "yourclass.h" // Always first
#include <libraryclass.h> // Now LibraryClass is completely specified

YourClass::YourClass(LibraryClass* c) : cl(c) {
  // Use LibraryClass here
  cl->foo();
}
Topic archived. No new replies allowed.