3

What's the preferred policy of header files in C++ projects: <test.h> or "test.h"? In my experience I use:

  1. #include <test_lib.h> to include C/C++ headers, third-part libraries (boost and etc.) which path specified in project settings.
  2. #include "my_test.h" - it's used for headers files existing in project.

Any other practices can be applied here?

Is it ok to include header files with relative paths: like #include "../new_test.h"? Or is it better to move relative paths to project settings?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Denis Solovov
  • 199
  • 2
  • 12

3 Answers3

2

Angle brackets are use to include system headers. Theoretically, IIRC, they're not even necessarily files. In practice, <> means do not search current directory, only the include path, whereas "" means look in current directory and then search the include path.

As for relative paths, it probably depends on your environment, I'd suggest path relative to your project include path (as specified by -I).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

This MSDN article explains how preprocessor searches for headers depending on the #include syntax form. Your style is good as Visual C++, Windows Framework, Boost and headers of other frameworks/packages are (usually) outside your project's directory.

Regarding your second question, my advice is to avoid relative paths. If you move around that header, you'll need to adjust that relative path by changing your code which is a bad idea. I'd rather put it within angle brackets and add path to it to /I option. E.g. you want to include C:\frameworks\frameworkA\include\a.h in your project, you can use #include <a.h> and \I "C:\frameworks\frameworkA\include"

It is maybe more descriptive if you add path to the framework root to /I and then put partial path to its header within angle brackets. E.g. #include <frameworkA\include\a.h> and /I "C:\frameworks". Now it's clear in the code that a.h belongs to frameworkA.

Use relative paths only for headers that are within your project's directory (e.g. if you organise code into modules which are in subdirectories of your project and which are not intended to be moved around).

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • +1 for the descriptive include bits, really helps when a project depends on several libraries! – Matthieu M. Nov 09 '11 at 10:23
  • Regarding the MSDN topic could you clarify this statement:" In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first." – Denis Solovov Nov 09 '11 at 11:17
  • To be honest I never relied on this rule but I guess that VC compiler "remembers" all previously resolved paths by placing them onto stack. If header is not found in step 1, preprocessor uses these remembered paths by going from the top of the stack where is the most recent path. IMHO, this rule (2) introduces some confusion as it allows that all headers with previously resolved paths can be included with quoted syntax.E.g.Assume that `b.h` and `c.h` are in `C:\foo` and you have `#include "a.h" #include #include "c.h"` and `/I "C:\foo"`.`c.h` will be found on the path resolved for `b.h`. – Bojan Komazec Nov 09 '11 at 12:14
  • Have a look at these links as well: http://stackoverflow.com/questions/179213/c-include-semantics http://www.eggheadcafe.com/microsoft/VC-Language/31858805/include.aspx http://code.google.com/p/waf/issues/detail?id=207 – Bojan Komazec Nov 09 '11 at 12:19
0
  1. You can use either. The <test.h> can be used by using the -I <directory containing test.h> option when compiling. In general, and this is a practice I follow, I tend to use <test.h> for all header files, irrespective of whether they are third-party header files or ones that I have written.

  2. It might be a good idea to refrain from using relative paths "../test.h". From personal experience, this way of writing #include statements forces you to cement your directory structure. If you decided to move test.h tomorrow to a different directory, you would have to go into each one of the header files and change the relative path for test.h to the new path - a time consuming exercise. Better to shift this to the makefile (via -I) and then deal with it from there.

Sriram
  • 10,298
  • 21
  • 83
  • 136