zen
zen

Reputation: 357

Visual Studio unable to find header file during compile despite include directory have been specified

After adding the include directory of the library which I am using. Visual Studio 2010 is able to find the header files I #included into my source code (IntelliSense does not show any errors). However when building the solution, it tells me that it wasn't able to find the header file. The same property used in my previous project does not post this issue.

The only solution I have now is to use the direct address for all the header files from that library, but I find it very irritating to do so, as the header files of the library cross reference each other and it does not make sense to edit all of them.

Does anyone have any idea what causing this problem?

Upvotes: 3

Views: 8932

Answers (3)

zen
zen

Reputation: 357

It seem like the actual problem is cause by me not adding the include directory in the project which was referring to the project which was implementing the library. This explain why I could build the referred project by itself and only having the problem when I compiled the solution as a whole.

I find this rather dumb to require us to re-declare the include directories in referring project when we have already done so in the referred project

Upvotes: 1

Mark Lakata
Mark Lakata

Reputation: 20818

It might because you have source + headers in 2 directories that refer to each other's header files. I.e. the files are

 1/a.c  
 1/a.h
 2/b.c
 2/b.h

and the contents of a.c and b.c have the same includes

 #include "a.h"
 #include "b.h"

Your project can find a.h when compiling a.c, and it can find b.h when compiling b.c (since the same directory is assumed in the search path when you use double-quotes in #include "xxx"). But a.c can't find b.h and b.c can't find a.h by default. Your project might be in directory 1 and you may have set up the include directory to look at 2. That works fine until 2/b.c needs to include "a.h". You need to set up the include directory path to include 1 as well as 2, even though 1 is your original project directory and it seems silly to do that.

This is a reason why IntelliSense can open the files (since it is omniscient), but the compiler can't (since it just looks at one file at a time).

Upvotes: 2

Daniel Rose
Daniel Rose

Reputation: 17638

IntelliSense uses a slightly different algorithm when searching for include files compared to the compiler & linker. In particular, it can also (sometimes) find header files even though the include directories are not properly specified.

I'll assume you specified the include directories correctly.

An idea: There's a bug in Visual Studio 2010 that if you specify a rooted path (ex. \myproject\includes), then when building the solution, VS uses the drive where it is installed (usually C:) rather than the drive where the solution is located. If this is the case, you'll have to either specify the drive (ex. D:\myproject\includes) or use a relative path (ex. ..\..\myproject\includes).

Upvotes: 1

Related Questions