27

In my current project I have separated my class files and my header files. My project structure currently looks like this:

  • Project

    • Source
      • src
        • class1.cpp
        • class2.cpp
      • main.cpp
    • Header
      • include
        • class1.h
        • class2.h

My problem is that I do not know how to include the header files into the class files. Am I unable to link to headers that are not at the same level or in a child folder? Or is there some way to go from the project root and work my way down? For instance:
#include "Project/Headers/include/class1.h" inside the class1.cpp file

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
Sheldon Allen
  • 403
  • 2
  • 5
  • 6

3 Answers3

28

Assuming you want class1.cpp to include class1.h you would do something like this

#include "../../Header/class1.h"

The .. tells the tells the OS to jump 1 directory up when the compiler asks for the file.

Pepe
  • 6,360
  • 5
  • 27
  • 29
  • 5
    The .. doesn't tell the compiler nothing. It tells the OS to jump 1 directory up when the compiler asks for the file. – Daniel Jul 30 '11 at 18:23
  • 3
    It's a bad practice to use ".." and "." when including header files. – mmohaveri Oct 20 '15 at 13:26
  • @mmohaveri can you explain why? – John McFarlane Aug 02 '18 at 03:06
  • @JohnMcFarlane first of all, it's not portable and it's not in the standard, so for example your code won't work in Windows. Second, It makes your code fragile, in the sense that a small change in your file locations can cause a build break. Finally, it sometimes cause your code to work magically (for more information check 'bk1e' answer on the following post) which in turn makes your code even more fragile. https://stackoverflow.com/questions/597318/what-are-the-benefits-of-a-relative-path-such-as-include-header-h-for-a-hea – mmohaveri Aug 10 '18 at 10:18
  • Re. portability: provided the file is not missing, `#include "../foo.h"` works for VC++ the same as for GCC and Clang. – John McFarlane Aug 10 '18 at 13:58
  • @mmohaveri Re. the standard: are you saying the standard in some way prohibits use of `..` for parent directories? – John McFarlane Aug 10 '18 at 13:58
  • @mmohaveri Re. code working magically: if you have a bug in your code and some feature causes there to be no diagnostic, that is not an argument against ever using the feature. I suspect that @bk1e's example is actually quite rare and overlooks the disadvantages of relying on absolute paths combines with `-I` directives to look up physically local files. Thanks – John McFarlane Aug 10 '18 at 14:06
  • @mmohaveri I posted [an answer](https://stackoverflow.com/a/52144130/671509) to that question. – John McFarlane Sep 03 '18 at 06:26
18

You need to indicate the include path <the directory containing Project> to your compiler so that the compiler is able to find the included headers. Using gcc, you could use -I option, and using visual studio, you could use /I.

amit kumar
  • 20,438
  • 23
  • 90
  • 126
1

I had a very similar problem where my compiler could not find the header with a code::blocks C++ project (same file structure as OP) .

This worked for me:

#include "../include/class1.h"
lm5050
  • 789
  • 7
  • 10