user3791372
user3791372

Reputation: 4695

Including files relative to project directory in c++

I have some header files contained in sub directories of the project source. Having to do #include "../../Foo.h" inside these files looks kinda messy in my opinion, when I'd much prefer to do #include "dir1/dir2/Foo.h" for faster human reading and understanding - is this possible, and if so, how?

Upvotes: 7

Views: 10539

Answers (1)

xtofl
xtofl

Reputation: 41519

Obviously you can. (And should)

Tell the compiler to add "projectdir/../.." to its include path. On Microsoft this would be using the /I switch, on gcc it's -I.

---- edit -----

In Visual Studio, you can use 'Macros'. In this case $(ProjectDir)/../.. should do.

Better still, if you can organize it that way: create a solution in the 'root' dir, add projects in subdirs, and use $(SolutionDir)/projectX/includes on your C++ include path.

Even more better still: add the dependent project as a Reference to your project. VS should automatically add its directory and lib to the compiler and linker settings.

Upvotes: 15

Related Questions