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?
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.
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.
In the current source directory. In the Additional Include Directories in the project properties (Project -> [project name] Properties, under C/C++ | General).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With