Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include Header Files When They Are In a Different Directory Structure

I have done some searching and found similar threads on the proper way to include header files in C++, but none of them quite answered this question.

I know that in order to include a header file in another folder you can simply use the following syntax:

#include "../myFolder/myFile.h"

But what about when the file is in a different directory structure a bit far removed? For example, if something like the following is guaranteed to be true:

Current directory = / f1 / f2 / f3 / f4 / f5 / myFile.cpp

Desired header directory = / f1 / d2 / d3 / d4 / d5 / d6 / myHeader.h

I know that you can set the "Additional Include Directories" property or use a make file, but I'd like to know if there is a way to do it from the #include statement.

like image 988
user1205577 Avatar asked May 21 '12 21:05

user1205577


2 Answers

putting ".." in #include is generally considered ugly and unmaintainable.

Every coherent library you're using (for instance boost) has a single root of header files hierarchy that you should put in your "additional include directories" property. For boost it is something like

`C:/lib/boost_1_49`

under this directory you usually find a directory called boost where all of the headers live. This brings the convention that all boost headers start with:

#include <boost/bla/bla.hpp>

This also goes for the very project you're writing. You should decide what's the best root for its headers and start all the includes from there.
The only exception to this rule should be headers that are in the same directory. Those can just be included as just a file name #include "same-dir-header.h"

You should also make the difference between including with "" and <>. Quotes should be things in your project, angle brackets should be external libraries (or as some would have it - OS and C runtime libraries)

like image 55
shoosh Avatar answered Oct 24 '22 00:10

shoosh


To complete the answer from @shoosh, you are supposed to tell your compiler where are those "other" header files. With gcc on windows, if they are in c:\path\to\library, then add the -I option

-Ic:\path\to\library

Beware of spaces in path, if the location is c:\my path\to\library, then:

-I"c:\my path\to\library"

Other compilers will provide a similar option, on command line or through the IDE.

like image 5
kebs Avatar answered Oct 23 '22 22:10

kebs