views:

93

answers:

2

What’s the preferred way to reference header files from cpp files that are in different sub-directories in a project? For example, bear.cpp in the forest directory needs to include lion.h in jungle. Is there a better way than using `#include “../jungle/lion.h”?

I would prefer to set an include path and simply use #include <lion>, but have two questions regarding this:

  1. Is this approach only recommended for headers that are part of a public API (Lion is an internal, project-specific class)

  2. Where do I specify an include path in Qt Creator?

+1  A: 

My preferred style is #include "jungle/lion.h" and jungle will be a namespace. The relative include paths (../) should be passed as a compiler option. (-I for GCC).

Vijay Mathew
+2  A: 

It is better to set the include path and use a short form. If you want to change the directory structure you only need to change it in one place and much more readable.

In Qt Creator add the following line to the project (.pro) file INCLUDEPATH += ../../dirname

Vereb