Reputation: 25
I just found out i've been doing this wrong for the whole time. I haven't used any ide and only use gcc. I have started using makefile also to compile my large project.
most of the time the file structure was this
├── makefile
└── src
├── folder1
│ ├── header1.cpp
│ └── header1.h
├── folder2
│ ├── header2.cpp
│ └── header2.h
└── main.cpp
on header2.cpp
, when I include header1.h
I do it like this
file header2.cpp
#include "../folder1/header1.h"
this is how I include the other files from another folder. I think I am doing wrong. most of the tutorial I have watch uses Ide and they don't include it like that.
some include it like this
#include "folder1/header1.h"
or others put it in a one folder like headers/
then include it like this.
#include "header1.h"
Can anyone guide me. how do i achieve this. I been doing this bad including I guess.
I don't want to include files like this
#include "../../../../sofarfolder1/header1.h"
thanks. it makes me puke everytime I see my code.
Upvotes: 1
Views: 2022
Reputation: 48665
How I deal with headers depends in if they are going to be installed (as with a library) or not.
Private headers I would keep in the project source folder:
├── Makefile
└── src
├── header1.cpp
└── header1.h
├── header2.cpp
└── header2.h
└── main.cpp
Then just include them like this:
#include "header1.h"
Public headers (to be installed) I generally put in a project subfolder like this:
├── Makefile
└── src
├── project
│ ├── header1.h
│ └── header2.h
└── header1.cpp
└── header2.cpp
└── main.cpp
And I include them like:
#include <project/header1.h>
In order to locate the public headers you need to set a compiler flag. For GCC
that is -I
g++ -Isrc ... etc ...
When the headers are installed they will go somewhere like /usr/include
:
── usr
└── include
├── project
│ ├── header1.h
│ └── header2.h
And client software will include them the same way:
#include <project/header1.h>
But they will supply different flag settings to find them:
g++ -I/usr/include ... etc ...
Upvotes: 1
Reputation: 17723
An unusual alternative to using compiler flags to specify additional include directories is to use the C Preprocessor to create defined constants for the include file path.
For instance if you have an include file with a relative path such as #include "../../stuff/lib1/thing1.h"
you can do something like the following.
#define THING1_H_PATH "../../stuff/lib1/thing1.h"
// ...
#include THING1_H_PATH
The gcc C Preprocessor documentation, The C Preprocessor in section 1.3.2, The #include Directive has this to say:
#include anything else
This variant is called a computed #include. Any `#include' directive whose argument does not fit the above two forms is a computed include. The text anything else is checked for macro calls, which are expanded (see section 1.4 Macros). When this is done, the result must fit one of the above two variants--in particular, the expanded text must in the end be surrounded by either quotes or angle braces.
This feature allows you to define a macro which controls the file name to be used at a later point in the program. One application of this is to allow a site-specific configuration file for your program to specify the names of the system include files to be used. This can help in porting the program to various operating systems in which the necessary system header files are found in different places.
Upvotes: 1
Reputation: 42888
You can use the -Idir
flag to tell GCC to look for header files in the directory dir
, if you don't want to use ../
.
More info: https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
Upvotes: 7
Reputation: 1706
https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html
In your makefile, you can invoke gcc with -I../../../sofardirectory
That way, it will look in that directory for headers you include.
Upvotes: 5