In a "working directory" I have a lot of *.cpp and *.h files that #include
each other and files from subdirectories.
For example:
#include "first.h"
#include "second.h"
#include "dir1/third.h"
#include "dir2/fourth.h"
In my own directory (that is different from the "working" directory) I would like to create a new *.cpp and *.h file that includes one of the files from the "working" directory. For example:
#include "/root/workingdirectory/first.h"
However, it does not work. Because "first.h" might include "second.h" and "second.h" is not located in my directory. Is there a way to tell the compiler that it needs to search included files not in the current but in the working directory: /root/workingdirectory/
?
To make it even more complex, dir1
and dir2
are not located in my working directory. They are located in /root/workingdirectory2/
. So, my second question is if it is possible to resolve this problem by letting compiler know that subdirectories are located somewhere else?
I also need to add, that I do not use any environment for development and compile from the command line (using g++
).
For gcc it is the
-I
option for header includes. For the .cpp files you just need those to appear as an argument to the gcc command.As you've already been told, it's useful to read the manual - specifically this chapter - and even more specifically right here.
Specifically, you want
Note also the documentation on
#include
directive syntax, described here as:So for example
will start looking in the same directory as the .cpp file containing this directive (or take a relative path as relative to this directory).
If you want to use the include path (specified by
-I
) you should useUsual practice is to use the
#include "local.h"
form for headers inside a library/package/module (however you've chosen to organize that), and the#include <external.h>
form for headers from external/3rd-party or system libraries.Read The Fine Manual
It's there for everyone to read. You even have a choice of what to use (I'd go with the first):