16

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 for algorithm/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 library

I 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 ?

Dipro Sen
  • 4,350
  • 13
  • 37
  • 50
  • 1
    "So should I use `#include "../x.h`" - Have you ***tried*** it? –  Feb 27 '13 at 19:12
  • What's your compilers (or more precisely preprocessors) opion about the path? – harper Feb 27 '13 at 19:13
  • I am using gcc, but it should work with msvc as well. but I've never seen any of these compilers refusing `../` in `#include` – Dipro Sen Feb 27 '13 at 19:19
  • @DiproSen My answer was deleted without comment. I hope it was useful while it was there. In response to you comment, I would check whether the algoithms really depend on x and y, or rather than traits presented by those classes? – Alex Chamberlain Feb 27 '13 at 19:21
  • I could have made the functions in algorithm templated. but in some cases the type is obvious, making those functions templated will be overdoing. So I need to include. But I need to group the algorithm related files also – Dipro Sen Feb 27 '13 at 19:24
  • Special case of [c++ - how to include header files in other src folder - Stack Overflow](https://stackoverflow.com/questions/5134357/how-to-include-header-files-in-other-src-folder). – user202729 Jan 29 '21 at 08:23

3 Answers3

9

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!

anatolyg
  • 26,506
  • 9
  • 60
  • 134
2

When making a library that I know I will use in other projects, I tend to use boost's inclucision style:

#include <libA/x.h>

This means that as long as the folder above "libA" (probably /include) is there, you can reference anything and everything beneath and with "libA". It also helps avoid collisions of similary-named inclusion files when you include things boost-style, because in your library and outside of your library's headers and other related code, you're always specifying the library you want to pull "x.h" from, e.g.

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

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

  • `#include ` not working either. Its complaining No such files. Now If I add INCLUDE PATH libA users will need to do the same otherwise it will not compile – Dipro Sen Feb 28 '13 at 04:23
  • @DiproSen Well, think about it. You have to do the same with boost as well; you add the toplevel boost folder to your INCLUDE PATH, and then use `#include `. There's no way to avoid that unless the user adds your lib files to a fundamental folder that the compiler always searches (which most users usually don't do). So, just tell them to add that include directory; it's the best you can do. –  Feb 28 '13 at 07:20
  • But then no one can just copy paste the folder in a subdirectory and start using it. – Dipro Sen Mar 01 '13 at 17:44
1

If you are using gcc, you can add -IPathTo/libA to add libA to the list of folders and then use #include "x.h"

zacaj
  • 1,987
  • 1
  • 19
  • 39