How to provide an additional file search order in a VC ++ project?

For example, I have two header.h files located in two different directories include1 and include2 . My source code uses regular inclusion, which does not determine the exact location, for example:

#include "header.h" 

In the project configuration, I set that in the list of additional included directories I include the folders include1 and include2 .

The problem is when I create my project, the include1 folder will be selected every time, no matter what order I defined in the list of additional inclusions.

Is there a way to force the search order if I want a specific folder or a specific header file to be used instead of the other if both have the same file name?

+7
c ++ c visual-studio
source share
2 answers

Inclusion order (as documented by MS):

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

So it really depends on where the include directories are declared. If both of them are specified with the / I switch (in the graphical interface, under "Configuration Properties"> "C / C ++"> "General"> "Additional Include Directories"), then the specified order is the search. If the directories are in the INCLUDE environment variable (in the GUI under Configuration Properties> VC ++ Directories), it depends on where they are declared. If you do not need to inherit them in the property sheets and declare them (and other inherited directories) yourself in the right order.

+9
source share

I think your problem will be difficult to solve. I suspect header.h is included from the file in the include1 folder.

This means that the standard path C includes

  • The directory of the source file (or the header file that includes the additional file).
  • The order specified on the command line includes.

So, to fix this, we would need to (somehow) stop rule 1 from firing.

Using #include "projecta/header.h" and #include "projectb/header.h" is the usual way to achieve this if you do not want to change the source.

In your case, you want yours (included2 included).

Ways you could apply.

If the previous code (.c / .cpp files) is mutable, then make sure that you #include "include2/header.h" - which should also set up so all requirements for include1 / header.h`.

if include1/header.h has an inclusion protector ( #if ! defined( H_HEADER_H) ), then define this on the command line ( /DH_HEADER_H ), so it is not included - it’s a blow in the dark, since the file will not fulfill its purpose.

Write a text modification script (sed?) To change "header.h" to "include2 / header.h", which ensures that all occurrences in the source select your version. This is not a specification, but it can be thought of as leaving the source code unchanged, as it will be a step to compilation.

The goal of changing the order of inclusion can be achieved by creating two separate projects - one that uses include1/header.h , and the source code does not change. It then suggests include2/header.h as an interface, which is being modified in some way.

0
source share

All Articles