0

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?

6
  • did you specify your compiler to search to the directory your files are included? Commented Nov 2, 2015 at 20:51
  • Strictly speaking a leading double underscore makes your program undefined but, that said, I don't think that will be causing the problem. Commented Nov 2, 2015 at 20:53
  • How are you including it? Commented Nov 2, 2015 at 20:54
  • @kcraigie just adding #include <Polygon.h> in the top of both files Commented Nov 2, 2015 at 20:55
  • 1
    Try #include "Polygon.h" Commented Nov 2, 2015 at 20:56

2 Answers 2

4

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.

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

3 Comments

I guess the better way is creating an Includes folder and set it as Include library and then i can use <>, its better when changing folder locations and stuff, am i right?
@OriRefael I did edit my answer, so hopefully, it is clear now.
As for "" vs <> there is another good answer worth perusing stackoverflow.com/questions/3162030/…
1

There you are, your compiler is searching the headers but he can't find them. Just go to: left click your project properties->c/c++ general->paths and symbols->includes in languages choose GNU C++, go to add button and add your include directory

2 Comments

in the Tool Strings i have C Compiler, C++ Compiler, Resource Compiler, Linker (link)
upvoted, that is educational and also could probably solve my problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.