Search code examples
c++eclipseeclipse-cdt

Eclipse c++ include header twice Error


I have the following files:

Polygon.h
Polygon.cpp
Rectangle.cpp
Triangle.cpp

Polygon.h is wrapped with

#ifndef __POLYGON_H
#define __POLYGON_H
#endif

Problem is: When I click 'Build All' Im getting an error in Rectangle and Triangle cpp files (they dont get a header file) alternately, its either one of them but never both.

Error's description:

C1083: Cannot open include file: 'Polygon.h': No such file or directory

Im using it twice because both Rectangle and Triangle inherit from Polygon, obviously.

EDIT

The including is done using #include <Polygon.h>

Halp?


Solution

  • If you write #include <Polygon.h> inside your code, that is a message for the compiler to look for Polygon.h inside its pre-defined includes (like language header files and maybe some others), but not inside current folder (of the file, which has the include written in it). You can tell the compiler (either using command line argument or by clicking it in the IDE) that it should also look somewhere else for the files being included using #include < > syntax.

    It is however better to write the #include little bit differently: #include "Polygon.h", because that means, 'look for it in current file's folder.', which is exactly what you want.

    The second #include syntax can be explained more generally. If you put a file inside double quotes, you can tell the full path to the file. Just the fact, that it does not start by / or C:\\ or similars means, look inside current folder.

    EDIT according to the comment.
    As far as I know, it is considered a better practice (according to most open source projects) to include headers, which are part of that project using #include "file.h" and headers, which are part of an external library using #include <file.h>. That is because your entire project (excluding any libraries) is an entire unit and the structure (if moved) will be moved at once and the structure will probably remain the same, throughout different changes to the project.

    Libraries on the other hand are there for themselves. You can assume, that someone (who will be using your project) already has the library installed (and maybe even customized). If they need to use your project, with their own library, they will only instruct compiler to compile your project, with their version of library, if you include it using #include <> syntax. Using the other syntax would put them into a trouble.

    You could put all headers inside one big folder and tell compiler to look in there, but imagine a project of hundreds header files. That folder will be messy and unmanage-able. Also, it's quite common to name your header so that you don't have to look inside to find out what does it do. Now imagine you need a queue inside your project and you'll end up with #include <queue.h>. What is the probability, that none of your external libraries (you are using throughout whole project) does not have a header named queue.h already? This way you'll end up with confused compiler, because it can't decide which queue you meant.

    Also, I believe that finding #include "file" will be a bit faster than #include <file>, but that is really small overhead.