Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce additional include files search order in VC++ project?

For example I have two header.h files located in two different directories include1 and include2. My source code file uses regular inclusion that doesn't specifies the exact location, like this:

#include "header.h"

In the project configuration I set my both include1 and include2 folders to be in the additional include directories list.

The problem is when I build my project, include1 folder will be chosen every time, regardless of the order I defined them in the additional include list.

Is there any way to enforce a search order, if I want a specific folder, or specific header file to be used, instead of another, if they both have the same filename?

like image 450
user1483597 Avatar asked Aug 24 '15 13:08

user1483597


People also ask

How do I add the include path in Visual Studio?

Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > C/C++ > General property page. Modify the Additional Include Directories property.

How do I include a .h file in Visual Studio?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

Where does C++ look for include files windows?

In the current source directory. In the Additional Include Directories in the project properties (Project -> [project name] Properties, under C/C++ | General).

What is %( AdditionalIncludeDirectories?

For a file, an item macro applies only to that file—for example, you can use %(AdditionalIncludeDirectories) to specify include directories that apply only to a particular file. This kind of item macro corresponds to an ItemGroup metadata in MSBuild.


1 Answers

The include order (as documented by MS) is:

The compiler searches for directories in the following order:
1. Directories containing the source file.
2. Directories specified with the /I option, in the order that CL encounters them.
3. Directories specified in the INCLUDE environment variable.

So it really depends on where the include directories are declared. If they're both specified with the /I option (in the GUI under Configuration Properties > C/C++ > General > Additional Include Directories), then the order specified is the order searched. If the directories are in the INCLUDE environment variable (in the GUI under Configuration Properties > VC++ Directories), it depends where they're declared. If it's in the property sheets then you'd have to not inherit them and declare them (and other inherited directories) yourself in the desired order.

like image 118
Avi Ginsburg Avatar answered Nov 12 '22 00:11

Avi Ginsburg