0

It's surely a noobish problem, I'm sorry for wasting your time, but I can't find any solution (I searched a bit on the internet, but found nothing).

I have a Console Application Project in Visual Studio 2013 and I have many folders where I put various .h and .cpp files. In these files I use namespaces for grouping classes.

The problem is that I can't include headers that are in parent directories in the current .cpp or .h file.

For example, I have a Hello class under the path /a/b/c, that is included in the namespace A/B/C, and a Hi class under /a/b/ that is in the namespace A/B. In my situation the Hello class doesn't "see" class Hi, but class Hi can "see" class Hello. Also, if I have Bye class in folder a/d/ with namespace A/D, class Bye doesn't "see" class Hi and Hello, and viceversa. What I mean for "doesn't see" is that #include doesn't find the headers and that when I use "using namespace ..." there aren't parent namespaces.

P.S. I hope my English is understandable (it's not my mother language) and that what I just wrote above is clear.

Aerdna
  • 1
  • 2
  • Did you set the project include directories? `Properties -> VC++ Directories -> General -> Include Directories` – NathanOliver Jan 21 '16 at 16:48
  • You can also use relative pathnames in your includes. – molbdnilo Jan 21 '16 at 16:49
  • Note that using many and nested namespaces isn't very common in C++, and in particular not "mirroring" a directory hierarchy with a namespace hierarchy. (And renaming namespaces when you decide to move a file is no fun at all.) – molbdnilo Jan 21 '16 at 16:52
  • @NathanOliver In the VC++ include directories I included my src (I used $(SolutionDir)src\) where I put all my files, but that doesn't work. I didn't know this property ,so have I used it correctly? – Aerdna Jan 21 '16 at 16:54
  • @molbdnilo I don't think that the problem is that namespaces reflects names of the directories, I have seen lots of people doing this and they had no problem. I also tried to copy their namespace/dir "layout", but that didn't help . – Aerdna Jan 21 '16 at 16:57

2 Answers2

0

In the Solution Explorer Window. Right click on your project and choose Properties from the menu.

In the C/C++ -> General settings the first field is "Additional Include Directories"

This is where you add paths for the compiler to load include files.

When the compiler encounters an #include statement, it tries to open the specified file. If the file is an absolute path, it only tries to load from that specific absolute.

If the file is a relative path, it tries to load from the directory of the file being compiled first. If the file is not found in the same folder as the c/cpp file, the compiler tries each of the paths in its 'Include Directories' list to find the file.

In visual studio, the 'Additional Include Directories' are based on the current project directory by default. You can obviously specify whatever you want using absolute paths and/or variables.

For your example you might want include directories something like:

a/b;a/b/c;a/d

acid1789
  • 139
  • 4
  • I used it, and I added the following: "$(SolutionDir)src\utils\" and "$(SolutionDir)src\graphics\" . In the first dir I have a Util.h and Util.cpp. Again I don't see it in my Graphics.h that is in the second dir . I tried to use #include "src\utils\Utils.h" and compile, but it throws a fatal error ("Cannot open include file: 'src\utils\Utils.h': No such file or directory"). – Aerdna Jan 21 '16 at 17:26
0

I found a solution for the problem. I used as a prefix in my #include the string "../../", then I can include all my files in my directories. I don't know if this the best thing to do, but it worked.

Aerdna
  • 1
  • 2