Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including files relative to project directory in c++

Tags:

c++

include

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?

like image 915
user3791372 Avatar asked Dec 20 '22 13:12

user3791372


1 Answers

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.

like image 161
xtofl Avatar answered Dec 31 '22 06:12

xtofl