views:

452

answers:

6

I am fairly new to developing C/C++ code in MSVS but one of the things that has already confused me is why, after adding a set of source and header files to my project such that they show up respectively under the MSVS folders 'Source Files' and 'Header Files', do I subsequently have to tell the compiler where my header files are under 'Project->properties->C/C++->General'. It seems to me that MSVS should already attempt to resolve any 'include "..."' statements by first looking thru the set of header files I have included in the project.

Anybody care to comment on the logic being used here?

Thanks, Travis

+2  A: 

Actually, adding your header files in the so called "Header Files" folder is optional. Even without doing the same you can just specify the header path in "Project->Properties->...." and it will still work.

You may be thinking from the perspective of only your project's header files which can be added in the "Header Files" folder, what about a big project having several third party libraries, you definitely cannot go and keep adding each every header file into your folder.

So, to keep all the includes unified at one configuration, this way should have been selected.

All the stuff quoted above is just my understanding. I don't have any evidence to support this. So, My apologies in advance is this is wrong. Don't bombard me with Downvotes please. : )

Jay
Thank you Jay for your response. I agree that large projects would still require the "Project->Properties" feature. That said, I still think having the project compiler automatically reference the project header files for #include resolution is an intuitive feature that could be easily implemented without conflicting with the need for additional external directories. I do recognize a certain logic in your argument that a unified configuration mechanism is desirable.
Travis
@Travis, If you would have noticed you can add files from any folder on your disk to your "Header Files" folder in IDE. Also, in your source file sometimes you may include the header as "#include folder1/myheader.h" for various reasons. In this case you need to give the path without including folder1 to the compiler. Now, for the IDE to guess it, the folder structure in the disk and IDE must map which will bring an unneccessary restriction in IDE. That said, if some more user friendly feature is brought in the IDE like you suggest I would definitely welcome it. : )
Jay
+2  A: 

The finding of header files has to be deterministic. There can be multiple files with the same name in your project. In that case, how would you want the compiler to solve this confusion ?

One other thing is that having directories where to look for header files is a historical feature of (pretty much ?) all compilers.

This leaves you with the only use of header files in your visual studio project : being able to open them easily from Visual Studio. That's it !

Benoît
Thank you Benoit for your response. Though I agree with you that the only use of header files in a VS project seems to be for ease in opening them, I think this is possibly a design flaw or at least non-intuitive. As I mentioned in a previous comment I am not sure what the purpose of the sub-header filter/folders are if not to provide seperate namespaces that mimic the deterministic #include paths in the source code. At any rate, I appreciate your assistance in helping me understand the VS development paradigm.
Travis
+1  A: 

First of all, have you created a new C/C++ project? If so it should create a directory at a place of your choice. Once this is done you can right click on your project (in the solution window in MSVS) and add a new file to your project. For example, you can add a source file (.cpp) and a header file (.h) to the project. Once this is done, you must write #include "yourfile.h" in your source file to be able to include the header file to your source file. Note that you can add new files and existing files. I hope this helps!

Partial
Yes, I have created a C/C++ project but I did so using the 'existing code wizard.' I was attempting to get a 3rd party open source code base to compile under windows. My goal was to NOT have to change any of the source code since this code base already compiled under Linux and MacOS. As such, I really didn't want to have to change the #include statements. Thanks for your response though.
Travis
I do not believe that have to change any code... If you create a new project, you simply need to add the existing files to the project and that is all.
Partial
+3  A: 

The project files are used by the IDE to keep track of your files, but the compiler doesn't have access to that information. The include file path is passed to each source file when it compiles.

Mark Ransom
While the question uses the phrase "tell the compiler", it's pretty clear from the context that he means in the IDE's project settings. It's not uncommon (even if it's imprecise, strictly speaking) to call the IDE "the compiler".
Michael Burr
Yes, thank you, I was speaking of the IDE. As an Integrated Development Environment I assumed it would integrate the project files with the compiler so that the details of command line processing would be in large part abstracted. I understand now that the IDE is in some ways a rather thinner veneer over the compiler than what I first expected. Thank you for responding.
Travis
+2  A: 

I think that it's because having a header in a directory doesn't necessarily mean that you want that directory searched for any other headers. That particular header could be included by specifying the path to the header, or it might be implicitly found because it's in the same directory as the file including it.

All that said (and it's just speculation), I think what you want is a reasonable request (it's something that's caused me a tiny bit of frustration before) - at least as an option or by being asked.

Michael Burr
As you say, it is not a major issue once the proper settings are realized. I only mention it because it seems like such an easy and intuitive behavior for the IDE to implement. If a developer goes to the trouble of adding include files to the project the IDE, in my opinion, should consider them for any #include statements. I did notice that the IDE allows sub-header folders (I think it calls them 'filters'?) to be set up so as to mimic the directory structure expected by the source code. Why do this if the full path must be specified in the project properties? Anyhow, thanks for your response.
Travis
A: 

i had never seen this problem.once you created a project and add any same project folder header file in IDE to your project,you can add header file by #include and compile your source code. if the desired header file doesn't exist in same folder that u create your project,u should add header directory in project properties as you said:)

celine