#include parent directory's file
Solution 1
Your solution with #include "../x.h"
will work. Regarding whether this is bad design - probably it is; hard to tell without knowing more about your code.
Consider the fact that if you have many include paths, the compiler/preprocessor will look for ../x.h
is all of them, which may be unintended and too broad!
Suppose you have the following directory structure, and Your_Code
is in a search path for include-files.
Unrelated_Directory/
x.h - unrelated
Your_Code/
libA/
x.h - the real one
algorithm/
a.h
This is dangerous. If you remove/rename your real x.h
, the compiler will silently pick Your_Code/../x.h
, which contains unrelated stuff - this can lead to cryptic error messages. Or even worse, this may be an old version, full of bugs!
Solution 2
If you are using gcc, you can add -IPathTo/libA to add libA to the list of folders and then use #include "x.h"
Dipro Sen
Updated on July 15, 2022Comments
-
Dipro Sen almost 2 years
My folder structure is
libA x.h y.h algorithm/ a.h
Now in
a.h
I have#include "libA/x.h"
which is not working. Its searching foralgorithm/libA/x.h
. So should I use#include "../x.h"
? Is the second option a bad design ? currently libA is header only. but latter I may choose to compile it as a libraryI am using cmake So can I or Should I add
libA
in my include path ?In short
some files in My algorithm directory needs to include definitions from its parent folder. I cannot make all functions templated because the types are obvious and it will be overdoing. So How should I design My project ?
-
zacaj almost 7 yearsCare to elaborate?