# include parent directory file

My folder structure

libA xh yh algorithm/ ah 

Now in ah I have #include "libA/xh" which does not work. His search for algorithm/libA/xh . So should I use #include "../xh" ? The second option is bad design? LibA is currently the only header. but the last thing I can choose is to compile it as a library

I am using cmake. Can I or should I add libA to my inclusion path?

In short

some files in my algorithms directory should include definitions from the parent folder. I cannot make all functions templates because types are obvious and they will overdo it. So how do I design my project?

+7
source share
3 answers

Your solution with #include "../xh" will work. As for poor design - perhaps this is so; it's hard to say without knowing more about your code.

Note that if you have many paths included, the compiler / preprocessor will look for ../xh - all of which may be unintentional and too wide!

Suppose you have the following directory structure, and Your_Code is in the search path for include files.

 Unrelated_Directory/ xh - unrelated Your_Code/ libA/ xh - the real one algorithm/ ah 

This is dangerous. If you delete / rename your real xh , the compiler will quietly select Your_Code/../xh , which contains unrelated things - this can lead to cryptic error messages. Or, even worse, it might be an old version full of bugs!

+4
source

When creating a library that I know, I will use in other projects, I tend to use the incucision boost style:

#include <libA/xh>

This means that as long as there is a folder above "libA" (possibly /include ), you can link to everything and everything below using "libA". It also helps to avoid collisions of similar inclusion files when you include boost-style things, because in your library and outside of your library headers and other related code you always specify the library from which you want to output "xh", for example,

 #include <SexyLib/xh> // Two different xh #include <TheLibFarAway/xh> // but same name! I hope you also have Namespaces :D 

This is only a personal preference, but it seems to work well for the libraries I'm developing and for boost . Hope this helps!

+2
source

If you use gcc, you can add -IPathTo / libA to add libA to the list of folders and then use #include "xh"

-one
source

All Articles