VisualStudio 2012 external includes visibility - c++

I have "source" and "include" folders which contain .cpp and .h files respectively for my project.
I have them in the same directory as the project.In the project I include the content of both folders and in the properties add the "include" folder as additional include directory.The project compiles but I can't access any header which is not related to these files via intellisense.For example GL\glew.h headers are included and used in my headers but I can't see those in the headers list when typing #include. Once I create a .cpp file outside "include" or "source" folder all the rest of project includes become visible there again.What do I miss here?

I finally solved this.Here is what was the issue:
It appears that using realtive path in VS2012 like this:
../../../SomeIncludeDir
Wouldn't really work for intellisence .At least for me.
I had to add this in the beginning :
$(ProjectDir)
So it goes like this:
$(ProjectDir)/../../SomeIncludeDir
This way VS can see the included dir ok.

Make sure to set path to the header files in project properties. Adding the headers to the solution explorer does not make them visible to IntelliSense.
Alt+F7 to open Property Pages > Configuration Properties > C/C++ > General > Additional Include Directories

Related

How do I include a header file located in a specific folder? (C++)

I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:
C:\\Projects\\MyProgram
The header file is located in the folder:
C:\\Projects\\MyProgram\\Files
I tried the following line of code, but it doesn't work.
#include <Files\MyHeader.h>
Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?
Thanks in advance for any help. :)
Try this
#include "files/myheader.h"
It will work if the header is in a files folder in the same directory as the current source.
If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).
In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.
Double Click on the Project Name. You will see a dialog box like this:
Make sure All Configurations is chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.
Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.
You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately
Add the library path the same way to Library Directories
Save the changes using the Save button on the Property Manager Pane's toolbox.
You will then be able to access the header file contained in the directory you added by something like:
#include <myheader.h>
This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.
The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.
The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.
If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).
Ideally you should use forward slashes in paths too.
Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir) I think.
That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.
To use a relative path in your cpp file without any include directory settings, you might need something like:
#include "../Files/MyHeader.h"
You just need to replace your brackets <> with double quotes "" like this:
#include "Files\MyHeader.h"
Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.

Cannot open include file with Visual Studio

I have recently gone from Code::Blocks to Visual Studio, and in Code::Blocks one could just add a class and then include it straight away. However, whenever I do the same in Visual Studio with the following statement:
#include "includedFile.h"
or
#include "include/includedFile.h"
It doesn't work and instead I get the error:
cannot open include file: 'includedFile.h'; no such file or directory.
Is there some box or setting that I have to tick? Or do I have to add each header as a dependency manually?
Here is the code for the class in question:
Public.h:
#pragma once
class Public
{
public:
static const int SCREEN_WIDTH=1000;
static const int SCREEN_HEIGHT=1250;
Public(void);
~Public(void);
};
Public.cpp:
#include "Public.h"
Public::Public(void)
{
}
Public::~Public(void)
{
}
How it is being included:
#include "Public.h"
I had this same issue going from e.g gcc to visual studio for C programming. Make sure your include file is actually in the directory -- not just shown in the VS project tree. For me in other languages copying into a folder in the project tree would indeed move the file in. With Visual Studio 2010, pasting into "Header Files" was NOT putting the .h file there.
Please check your actual directory for the presence of the include file. Putting it into the "header files" folder in project/solution explorer was not enough.
Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directories add the path to your .h file.
And be sure that your Configuration and Platform are the active ones. Example: Configuration: Active(Debug) Platform: Active(Win32).
You need to set the path for the preprocessor to search for these include files, if they are not in the project folder.
You can set the path in VC++ Directories, or in Additional Include Directories. Both are found in project settings.
By default, Visual Studio searches for headers in the folder where your project is ($ProjectDir) and in the default standard libraries directories. If you need to include something that is not placed in your project directory, you need to add the path to the folder to include:
Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directories add the path to your .h file.
You can, also, as suggested by Chris Olen, add the path to VC++ Directories field.
I found this post because I was having the same error in Microsoft Visual C++. (Though it seems it's cause was a little different, than the above posted question.)
I had placed the file, I was trying to include, in the same directory, but it still could not be found.
My include looked like this: #include <ftdi.h>
But When I changed it to this: #include "ftdi.h" then it found it.
If your problem is still there it's certainly because you are trying to compile a different version from your current settings.
For example if you set your Additional Include Directories in Debug x64, be sure that you are compiling with the same configuration.
Check this: Build > Configuration Manager... > There is problably something like this in your active solution configuration: Debug x86 (Win32) platform.
For me, it helped to link the projects current directory as such:
In the properties -> C++ -> General window, instead of linking the path to the file in "additional include directories". Put "." and uncheck "inheret from parent or project defaults".
Hope this helps.
I tried the other answers here as well, but my problem had nothing to do with the include paths or files missing incorrect #includes. I had two configurations, each set to the exact same include directories. One configuration could resolve the includes, the other could not.
After selecting my project and going to Project -> Properties, I selected both configurations through the Configuration dropdown -> Multiple Configurations... option. Comparing the two I found that C/C++ -> Language -> Conformance Mode was different. The "incorrect" configuration had a value of Default for some reason, and switching it to Yes or No allowed the paths to be resolved.
TL;DR: If you have one configuration with the same include directories but the other isn't finding the files, I suggest to try comparing the configurations.
If you've tried the other answers and your include file still can't be found, here are some additional debugging steps and sanity-checks:
Ensure that you are building to a platform that is supported by your code. (If not, consider removing this platform as a target)
Verify that the filename/path is correct. Modify your source code to #include the whole absolute path of the header file instead, and see if the file can be found now. If not, copy-paste the path from your source code into a command line to validate that the file exists at that full path with no typos. Open the header file to ensure you have read access. (Change the source code back when done.)
If you've already added the path to Additional Include Directories, try clicking the drop-down combo box for Additional Include Directories, and select <Edit...>. This will show you evaluated values of paths. (If it does not show the correct evaluated values, variables in your path might not be set. Click Macros>> to see variables.) Copy-paste the evaluated path into windows explorer to validate that the path exists.
Create a new empty C++ "Windows Console Application" project. Set just the one Include Directory, and #include just the one file in your main.cpp, and see if that builds.

How to include the stdafx.h from the root directory?

With "Show all files" option on in VS, i added a folder and created a new class in that folder. Since i'm using precompiled headers i also need to include the stdafx.h that's in the root directory relative to the new class file.
In my cpp file i have
#include "..\stdafx.h"
Yet I get the following error:
error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
My understanding is, that the .. should instruct the compiler to go one directory level up ?
Visual C++ allows you to define several ways of setting up precompiled header files. The most common is to enable it for ALL source files at the project configuration level, Under Configuration Properties/C++/Precompiled Headers, setting "Precompiled Header", select "Use". The same location, setting "Precompiled Header File", is usually "stdafx.h". All files will get this setting (thus the configuration at the project) EXCEPT....
One file is responsible for generating the PCH file. That file is typically the stdafx.cpp file in your project, and it typically has nothing in it except #include "stdafx.h". Configuring Precompiled Headers for THAT ONE FILE, switch from "Use" to "Create". This ensures that if the prime-header for PCH gets out of synch stdafx.cpp is ALWAYS compiled first to regenerate the PCH data file. There are other ways of configuring PCH setting in Visual Studio, but this is the most common.
That being said, your problem is definitely irritating. The filename used to prime the PCH system and specified on both the "Use..." and "Create..." setting above MUST MATCH THE TEXT IN YOUR #include EXACTLY.
Therefore, it is highly likely you can address your problem by adding ".." to your project include directories and removing the ".." from your #include statement. you could also change it at the project-configuration level to be "..\stdafx.h" as the through-header, but that might be a problem if you have source files in multiple folders hierarchically.
Oh, and if it wasn't clear to you while perusing the PCH configuration settings, if you do NOT want to use PCH for any specific source file (and there are reasons not to sometimes) you can turn it OFF for specific source files, otherwise be sure to always have #include "your-pch-include-file.h" at the head of every source file (c/cpp,etc).
Hope you catch a break.
I generally also like to have a hierarchical order in my projects, and I've found there are two simple ways to include a precompiled header:
Either
Put the directory where stdafx.h lies into the compiler's include directories.
(Properties - VC++ Directories - Include Directories: Add $(ProjectDir))
Or
If there aren't too many subdirectories, a simple way to circumvent the error message is like this:
Put an stdafx.h file into each of your subdirectories which only includes the top-level stdafx.h:
#include "..\stdafx.h"
Write #include "stdafx.h" as first line of all source files in your subdirectories, instead of including the top-level file there.
This way, all your code files use the same precompiled header file, and there is no other complicated setup to do.
It's interesting that the trick that I use isn't in the answers:
Create stdafx.h and stdafx.cpp in the root folder of the project.
Go to project properties -> precompiled headers. Change to "use".
Go to stdafx.cpp, right-click properties -> precompiled headers. Change to "create".
Go to project properties -> advanced; change "Force include files" to stdafx.h;%(ForcedIncludeFiles)
Don't change any CPP file; keep your header files as they are. Build as-is.
No typing, no RSI, no hassle with include paths, no other pain and misery. And the beauty is that it will still work when you move your solution to another platform. Awesome.
You can adjust the precompiled header settings on a per-file basis.
In Solution Explorer right click on the .cpp file, select "Properties".
I'd strongly recommend selecting "All Configurations" in the Configuration drop down List item.
Browse to "C/C++" - "Precompiled Headers".
Adjust the "Precompiled Header File" from "stdafx.h" to whatever you need (in your case for example "../stdafx.h").
Note this is tedious and error prone since it's done on a per-file basis, and future developers adding files to your project will have to follow the same steps. If they don't they will be faced with warnings and errors such as:
warning C4627: '#include "<path>"': skipped when looking for
precompiled header use.
and
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
which don't give much indication as to this or any other approach.
I guess they'll eventually turn to StackOverflow and end up here... Hello, thanks for reading.
On that basis, it's worth going with alternatives, such as putting $(ProjectDir) on the C++ Include path (under C++\General) but then that can cause confusion when including other header files.
PCH files are wierd, and even moreso in Visual Studio. When compiling a .cpp file that uses a PCH, VS expects the first non-comment/whitespace text to be #include "PCH_NAME_HERE". The PCH_NAME_HERE is exactly the PCH's name. No directories, nothing. Just the PCH's name as specified in the compiler options.
If you're going to do this directory fudging, then you need to modify your compiler settings so that the directory where the PCH is is in the directory search path. That way, you don't need to have the ..\ part.
I would suggest to use:
$(ProjectDir)/pch/my_pch.h
as "Precompiled Header File"
and "Advanced > Force Include File"
This will auto include pch for your .cpp files in the beginning, so nothing needs to be changed in the .cpp files.
And this is better than changing the include directory, because sometimes you might have multiple pch files in the include directories, and then you can't tell which one has been used.
The cause of the confusion is that Visual Studio treats the include directive that includes the precompiled header differently to other include directives. Specifically it does not look for the precompiled header using the normal path lookup approach, instead it simply attempts to match the include directive to that defined in the project configuration by simple string comparison.
Precompiler header configuration is set gobally but can be overridden per file. The normal global config (accessed via Project Properties -> Configuration Properties -> C/C++ -> Precompiled Headers) is:
Precompiled Header: Use (/Yu)
Precompiled Header File: stdafx.h
Precompiled Header Output File: $(IntDir)$(TargetName).pch
This configuration is applied to all files in the project by default. However the config for stdafx.cpp is set at the file level and overrides the Precompiled Header value to:
Precompiled Header: Create (/Yuc)
The effect of this is that for any source file configured to use the precompiled header (which by default is all of them except stdafx.cpp) VS will look for an include directive that matches the configured Precompiled Header File value. e.g.
#include "stdafx.h"
Because the check uses a simple string comparison instead of any kind of directory search, then (irrespective of the location of the source file relative to the project root directory or the location of the stdafx.h file) the path and filename used in the include directive must match exactly that used by the project's Precompiled Header File configuration setting. The unexpected side effect of this is that if you have a project subdirectory containing various source files, in those files you do not need to reference the stdafx.h file using a relative path like ..\stdafx.h (and if you do VS will raise an error stating that it encountered the end of file while looking for the precompiled header).
Just use the unadorned #include "stdafx.h" and it will work fine because VS will then recognise this as the directive to use the precompiled header, and it already knows where the correct precompiled header is because of stdafx.cpp Precompiled Header configuration being set to "Create (/Yc)".
If .cpp and .h files of your project live in different subdirectories (not plainly in the directory of the project), it would be a good coding style to use include paths relative to the solution directory (if you don't use a dedicated include directory). Particularly if you have multiple projects in a solution and need to share include files (e.g. for interoperability between projects, e.g. an .exe and a .dll).
To refactor your project you need to do the following:
In each project specify additional include directory
$(SolutionDir) : right-click on project, click "Properties", go to
"Configuration Properties"->"C/C++"->"General" (to do this for all
configurations at once, select "All Configurations" from the
"Configuration" dropdown)
Go to "C/C++"->"Precompiled Headers"
and change "Precompiled Header File" value to the path relative to
the solution directory, e.g. PROJECT_NAME/stdafx.h
In your .cpp
files include "PROJECT_NAME/stdafx.h", instead of just "stdafx.h"
In your .h and .cpp files, when including something, use path as
"PROJECT_NAME/dir1/dir2/file.h", except when including file from the
same directory
Using quotes means it is a header file you own use <> means it is a system header file if I am not mistaken just use #include <stdafx.h> and let the compiler find it

VS2003 can't find .h file even after addition to "additional incl. dirs"

I have file which needs to include blah.h in another project. I've tried adding the directory that holds the blah.h file to
Project -> Project Properties -> Additional include directories
but I keep getting a "C1083: Cannot open include file: blah.h: No such file or directory". I've tried absolute and relative paths to the folder housing blah.h with no success.
I'm using Visual Studios 2003. and my include statement looks like
#include "blah.h"
Any suggestions?
Get the build (or at least this part of the build) working using:
#include "c:/path/to/blah.h"
If/when VS stops complaining about not being able to find blah.h, cut-n-paste the path used in the #include into the "Additional include directories". Remember that the "Additional include directories" field should have the directories separated by a semi-colon.
Also, make sure that you set the "Additional include directories" property for all appropriate build configurations and platforms (drop-downs at the top of the Project property page dialog). Since the dialog opens up with only the "Active" configuration selected, I often find myself setting up an option for only the "Debug"/"Win32" configuration, then wonder for a second what's gone wrong when I get errors after switching to the "Release" configuration or "x64".
For problems like this I use ProcessMonitor, a free utility from Microsoft. Among other things, it logs accesses to files. Filter on blah.h, see what path Visual Studio is really using, then adjust your "Additional Includes" as needed.
Ok here was my problem and the solution I came up with:
I have a multi-project solution in VS2003. In one project I have a templated class Child defined in Bar.h which inherits from a non-templated class Parent in Foo.h. Parent has a static member some_mutex declared in it. This meant that I needed to define some_mutex in Foo.cpp.
The problem arose when I tried to include Bar.h in files in other projects, e.g. other.vcproj. Without adding the include path to Bar.h in other.vcproj I'd get a C1083 error because the compiler couldn't find the definition for Child. But when I added Bar.h's include path to other.vcproj I'd get a LNK2001 error for some_mutex because Foo.cpp was in a different project and was not getting included/compiled. This meant that some_mutex was undefined and that caused the LNK2001 error.
The solution was to put Foo.h, Foo.cpp, and Bar.h in their own project NewProj and create a static library. The other projects that wanted to use the class Child just needed to include NewProj.lib, add the path to NewProj.lib, and add the include path to Bar.h.
If you have entered the path to the "blah.h" directory as a relative path, be aware that VS2008 interprets those paths relative to the directory holding the .vcproj project file. So you'll need to work out what that would be.
If your C++ file is "C:\myprogram\source\foo.cpp" and your include file is "C:\myprogram\includes\blah.h", it is not adequate to provide an include path of "..\includes". You have to figure out where your project file is. If it's "C:\myprogram\build\mysolution\myprogram\myprogram.vcproj" then the include path must be "..\..\..\includes".

How to include header files in Visual Studio 2008?

I am currently trying to compile a simple program that includes two header files. I see them in the Solution Explorer, where I included them through "include existing files". However, when I run my program it get the following error.
fatal error C1083: Cannot open include file: 'FileWrite.h': No such file or directory. THe problem is that I see the file included in the Header's folder and in the code I have written:
#include "FileWrite.h"
and then the rest of the program code.
Is there something else needed to do so that the compiler can see the header file and link it to the .cpp file I'm trying to compile?
If you write in your code something like #include "FileWrite.h" you need to make sure compiler can find that file. There are three options:
FileWrite.h should either be in the same directory as your source code file (.cpp) or
Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories) or
Path could be set in your VisualStudio - add it to Include Files in Tools->Options->Projects and Solutions->VC++ Directories
Which of these options shell be used depends on whether that header originates from this project (1st option) or some other project (any of other two options).
There are two ways to do this.
1) Only for the current project
Select your project -> properties -> C/C++ -> General -> Additional Include Directories -
Include your header file directory.
2) For all projects
Tools -> Options -> VC++ Directories -> Include files - Add the header file directory.
Refrain from using 2, as it would be difficult to figure out dependencies for a project when compiling it on a system different than yours.
When including files the compiler first looks in the current directory (the directory which contains the source .cpp file) then it looks in the additional include directories. If FileWrite.h isn't in the same directory as your source file check the additional included directories.
In the project's property page look at the additional include directories and see if they include the folder in which FileWrite.h is in.
You said the file is in the "headers" folder. This could either mean the headers filter or an actual headers directory on the filesystem. When including a file from your own project you need to specify the path from the file you're including into. So, if you had something like so:
src/main.cpp
include/my_object.h
You would use #include "../include/my_object.h" in main.cpp.
That's for directories. The folders you see in your project are called filters and have absolutely no relation to the directory structure of your project unless you force it to. You need to be paying attention to what the structure looks like in windows explorer to ascertain what path to use in an include statement.