Visual C++ include order incorrect

Added by Anonymous about 7 years ago

I just ran into an issue where #include <string.h> is including my String.h file, instead of the one from the C Standard Library. It looks like the order of the /I switches is my project first, and then the includes from the Windows SDK (including the standard library).

According to MSDN:

"If more than one directory appears as part of the /I option or within the INCLUDE environment variable, the preprocessor searches them in the order in which they appear."
http://msdn.microsoft.com/en-us/library/36k2cdd4(v=vs.110).aspx

It seems like that is the cause of my issues. In my case, I'm actually including <cstring> which then tries to include <string.h> and that is picking up my file. This works normally within a Visual C++ project, despite having the same name.

On a side note, I noticed the include order for Visual Studio 2012 is slightly different than what I'm seeing by creating a new C++ project. The $(WindowsSDK_IncludePath) macro includes "um", "shared", and then "winrt" from Windows SDK, but JamPlus has "shared", "um", and then "winrt". I haven't run into any issues with it yet, just pointing it out.

I appreciate any help with this issue. I'd be happy to test out swapping the order to ensure that is indeed my problem, but I can't for the life of me figure out where this is controlled.


Replies (5)

RE: Visual C++ include order incorrect - Added by Joshua Jensen about 7 years ago

Actually, the Jam build isn't architected to adhere directly to the method that Visual Studio uses to search include paths. There are a variety of compilers, and JamPlus used the best compromise.

However, in removing the compromise, I can't see any real behavioral differences except in source files that try to access the precompiled header but reside deeper in the directory hierarchy than where the precompiled header file resides. The solution, in this case, was a simple call to:

    C.IncludeDirectories target : . ;

So, I have pushed a change that brings this in line with what you describe above. I may revert it if someone complains. This behavior has been in JamPlus for 4+ years now.

As far as the $(WindowsSDK_IncludePath) order, the JamPlus order comes from looking at the INCLUDE environment variable in the VS2012 command prompt. I don't care what order it is, but that is where I chose the "shared", "um", and "winrt" order from.

-Josh

RE: Visual C++ include order incorrect - Added by Anonymous about 7 years ago

That didn't seem to fix it for me. I put together some files which can repro the problem. It's setup to match my include structure (Include/System/Subsystem) just in case that has any effect.

But basically Main.cpp includes <cstring>, which then includes <string.h> and that is picking up my file, as seen by the message printing during compiling.

I appreciate your help with this.

RE: Visual C++ include order incorrect - Added by Joshua Jensen about 7 years ago

Okay, so I have attached a Visual Studio 2010 solution and project that unzips in the root of the JamPlusIncludeTest directory. When I build with it, I get the behavior where <cstring> includes JamPlusIncludeTest/Include/Test/Foot/String.h.

This is the JamPlus behavior, too.

Can you alter the Visual Studio project and show me how I set it up wrong?

Thanks!

Josh

RE: Visual C++ include order incorrect - Added by Anonymous about 7 years ago

Looks like the test I setup before starting this thread must've been wrong, because I am seeing the same results you are.

I was pretty confident this Jam file is how everything was setup when I used Visual Studio projects, but after digging through my source control, I found out that's not the case. I have Include and Source/Test/Foo as include directories, but not Include/Test/Foo. It was the extra Include/Test/Foo causing the issues. When I removed that from my Jam files, everything works like a charm.

Sorry about that, not sure what I did wrong with my test setup so I didn't see the issue. But thanks for leading me to the solution :)

RE: Visual C++ include order incorrect - Added by Joshua Jensen almost 7 years ago

Joshua Jensen wrote:

Actually, the Jam build isn't architected to adhere directly to the method that Visual Studio uses to search include paths. There are a variety of compilers, and JamPlus used the best compromise.

However, in removing the compromise, I can't see any real behavioral differences except in source files that try to access the precompiled header but reside deeper in the directory hierarchy than where the precompiled header file resides. The solution, in this case, was a simple call to:

So, I have pushed a change that brings this in line with what you describe above. I may revert it if someone complains. This behavior has been in JamPlus for 4+ years now.

This change broke precompiled headers on GCC compilers. I have pushed a potential fix.

-Josh

(1-5/5)