简体   繁体   中英

Where does Visual Studio's compiler search for #includes?

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.

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

When you write #include "ah" in a.cpp , then preprocessor searches ah 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 ah .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM