Visual studio: including files from a far away directory

Started by
6 comments, last by Drew_Benton 12 years, 11 months ago
I have a fresh solution and I used "add existing file" to add my assert header to my project from a few directories away. I get no error for the #include, but it can't find the contents of my file. Not sure whats up.
Advertisement
What do you mean "can't fiind the content"? Does it add to the project, complain when you try to view the file, or does it moan when you try to compile a file that includes the header? Is the include file itself trying to include other, distant files? Sounds like to need to add the directory as an additional search path. Need more details really.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

It's a header with 3 assert macros that includes stdio.h. I simply can't use the macros because the compiler can't find their definition and gives me the "identifier not found" issue.
Whats the header called? do you also have another file with the same name in the soluton directory? All I can think is whatever file is including it, is dragging in another file, instead of the one you add to the solution.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

It's called Assert.h. I don't have a file with the same name anywhere else.
Not sure the term to use but thats a standard file of sorts, so its probably including the one from your vs install directory. I strongly suggest you rename the file. Are you including it with angle brackets or quotes?

#include <assert.h>
#include "assert.h"

if your using angle bracket then try switching to quotes.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

I tried a different file in the same situation, and for that one it did not find the file to include, so you must be right about it finding a different file. I had to add the additional include directories to my project to get it to work. Switching between quotes and angle brackets did not help. Why can VS not find a file if I include it in a project? Why should I have to also add include directories?

Why can VS not find a file if I include it in a project? Why should I have to also add include directories?


Shorter Reason

The way C/C++ works makes it very impractical to automatically include the entire path to a folder just because you add a file from it to the workspace. It would cause too many ambiguities of which files should be used if duplicate across the paths. When you add a folder path to Visual Studio, the order matters! If the IDE simply added paths in the order you added include files to the project, there is a high chance the project would break from having files with the same name.

The fact programmers also choose to use header names the same as standard library names doesn't help either. ;) How can the IDE work out that you want to use your header or theirs automatically at compile time for different files (both system and user) (i.e., pass the right paths to the compiler)? The problem is really a lot more complicated than it might seem at first glance. As a result, it is setup how it currently is.

Longer Reason

Because "include files" are not a stand-alone compilation units. (Note: That is not to be confused with precompiled headers)

If you add a .c or .cpp file from any path to your project, the path can be used by Visual Studio to compile the file.

Header files are embedded into the file that includes them through the preprocessor at compile time (hence the #include required!).

In order for the compiler to find the file, you have to give it the correct path.

#include <assert.h> - Means the file is in a registered directory with Visual Studio.
#include "assert.h" - Means the file is in a relative path to the file including it.

The "correct" path to use when it is not in a registered path would be:
#include "Drive:\\Path\\To\\Your\\File\\assert.h" - Absolute
#include "..\\..\\Path\\To\\Your\\File\\assert.h" - Relative (go 'up' so many directories then take the standard path)

With all this said, notice how you can use header files in your code without including them into your project workspace! They are only there for convenience and serve no other purpose, outside of providing some intellisense information and quick code lookups.VS2010 has added a neat feature to detect header file dependencies and adds them to the workspace for you. However, this functionality depends on the previously mentioned folder path setup to work correctly.

This topic is closed to new replies.

Advertisement