0

If I simply include a file by writing

#include "blah.h"

where exactly does the compiler search for this file? I understand that there are limitations.

What happens if the file is not in the same folder, but much deeper in the structure, how do I tell the compiler to look there? equally if it is above the file in the directory? or maybe deeper in a different branch?

Essentially I don't have a grasp of how you navigate around the structure. I've seen some includes that look something like:

#include ".././foo/whatever/blah.h"

what do the dots mean? they go back up but do different numbers mean different things?

Also is this based on the structure of the files on the computer or their structure in the solution explorer?

Thanks very much for the help on this one, I understand this is a bit of a basic question - just one of those things I never learned.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dollarslice
  • 9,917
  • 22
  • 59
  • 87
  • There's an setting under project properties, which is called "Include directories" (in different versions of vs it's under different sub-branch, in vs2010 it's under Configuration Properties->VC++ Directories). Always the dots in "include" go back up based on the structure of the files on disk, starting from the location of the source file which included them (and also if not found there, then it's searched starting from the location of every directory in "include directories" but again by physical location on disk. – user1227804 Mar 01 '12 at 17:54

2 Answers2

2

From the visual studio documentation on #include


The preprocessor searches for include files in the following order:

  1. In the same directory as the file that contains the #include statement.

  2. In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.

  3. Along the path specified by each /I compiler option.

  4. Along the paths specified by the INCLUDE environment variable.


You can change the paths passed to the compiler via /I options in the visual studio project settings (for project specific paths) and in the visual studio options for global paths.

2 dots in a path move you up a directory, one dot refers to the current directory. Any other number of dots would not be valid. So your example path of .././foo/whatever/blah.h essentially means "move up one level, look in a folder foo, then look in a folder whatever". The single dot in this case doesn't really do anything.

This navigation is based on the file structure, not the structure in solution explorer

obmarg
  • 9,369
  • 36
  • 59
1

When you write #include "a.h" in a.cpp, then preprocessor searches a.h in the same directory where a.cpp is. If this search is not supported, or if the search fails, then preprocessor searches a sequence of implementation-defined places for this a.h.

"what do the dots mean?"
Lets say you have file with this fullpath: C:\myDir\myProjects\a\a.cpp:
. = C:\myDir\myProjects\a
.. = C:\myDir\myProjects

You should also take a look at What is the difference between #include <filename> and #include "filename"?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167