Search code examples
c++organization

How to include different headers from different folders?


I have a C++ project and I love mycode organized, however when I put everything in separate directories (headers and source files kept together) I am not able to include headers from another folder. For example: I have two classes called "FooException" and "ContentProvider", which obviously go into separate directories, FooException being an exception and ContentProvider being a utility. Of course, I put them into different folders, but when I try to include the FooException in the ContenProvider it does not work and says that it could not find the source file. Has anyone encountered such problem? Any suggestions? Thanks in advance and Happy New Year!

UPD: Okay, people suggested looking at the differences between #include <> and #include "" and that still did not help. Although I am able to access any files from my Main.cpp I am not able to access files neither in my ContentProvider class nor in FooException class no matter which #include-statement I use. Moreover, no matter which statement I use I can access SFML-library from any point in my project. Does it have to do with the SFML-directory being in the "include"-directory in my project?

UPD 2: Okay, problem solved and it had nothing to do with #include <> and #include "". I just had to put "..\" before writing the name of the path and it worked beautifully. I marked the answer that suited the best right.


Solution

  • You can:

    1. Add the directories which have the required header files as part of pre-processor lookup. Different compilers have different way to achieve this.
    2. Add the header file as a relative path. For eg. #include "../folderName/A.h"
    3. Combine 1. and 2. Add a top level directory in the search path and include files as #include "topLevelDirectory/A.h"

    I personally do not like 2. because if your file hierarchy changes you will have to modify source code to change the relative paths.