#include header files when they are in a different directory structure

I did a search and found similar streams to include C ++ header files, but none of them answered this question.

I know that to include the 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 little deleted? For example, if something like the following is guaranteed:

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 the make file, but I would like to know if there is a way to do this from the #include statement.

+7
source share
2 answers

put ".." in #include usually considered ugly and unreachable.

Each linked library that you use (such as boost) has one root in the header file hierarchy, which you must put in your "extra included directories" attribute. To enhance it is something like

 `C:/lib/boost_1_49` 

in this directory, you usually find a directory called boost , where all the headers live. This causes all header headers to begin with:

 #include <boost/bla/bla.hpp> 

This also applies to the project you are writing. You have to decide what is the best root for your headlines and start all inclusions from there.
The only exception to this rule should be headers that are in the same directory. They can simply be included as the file name #include "same-dir-header.h"

You should also make the difference between inclusion with "" and <> . Quotations must be in your project, angle brackets must be external libraries (or, as some of them are, OS and C runtime libraries)

+10
source

To complete the response from @shoosh, you must tell your compiler where these "other" header files are located. From gcc on windows, if they are in c: \ path \ to \ library, add the -I option

 -Ic:\path\to\library 

Beware of spaces in the 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 the command line or through the IDE.

+1
source

Source: https://habr.com/ru/post/916304/


All Articles