Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including header files style - C++

I have a project which has the following directory structure.

root
--include
----module1
----module2
--src
----module1
----module2

So a file say foo.cpp in src/module1 has to include like,

#include "../../include/module1/foo.hpp"

This looks messy and tough to write. I found writing include like

#include <module1/foo.h>

and providing include file search path to root/include when compiling looks neat. However, I am not sure that this style has got any drawbacks.

Which one do you prefer and why? Also do you see any problems in organizing files in the above way?

like image 425
Navaneeth K N Avatar asked Jan 28 '10 17:01

Navaneeth K N


1 Answers

#include "../../include/module1/foo.hpp"

Specifying paths should be avoided as much as possible. Compilers provide you with a cleaner alternative to achieve the same. Further, a clean design should see to it that you do not need to juggle relative paths for including headers.

A better idea of which one to use (including whether to use quotes or the angle-brackets) can be had from the standard.

From my copy of the C++ draft:

16.2 Source file inclusion

2 A preprocessing directive of the form

#include <h-char-sequence> new-line`

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

3 A preprocessing directive of the form

# include "q-char-sequence" new-line 

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

#include <h-char-sequence> new-line`

with the identical contained sequence (including > characters, if any) from the original directive.

7 Although an implementation may provide a mechanism for making arbitrary source files available to the < > search, in general programmers should use the < > form for headers provided with the implementation, and the " " form for sources outside the control of the implementation.

like image 158
dirkgently Avatar answered Sep 29 '22 19:09

dirkgently