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.
Related
I'd like to use the image processing package of Dlib (C++ library) in Visual Studio 2013.
I created an empty project and added "dlib-18.16\dlib\all\source.cpp" to my Source Files in the Solution Explorer. Then, I added the path to dlib-18.16 to my Include Directories in VC++ Directories and I also added the path to dlib-18.16\dlib to my Additional Include Directories in C/C++ General of Visual Studio.
I can run the file matrix_ex.cpp which is one of the examples of Dlib, but I can't run the file face_detection_ex.cpp because of the error " Cannot open include file:'type_safe_union/type_safe_union_kernel.h' " which is actually caused by the line #include <dlib/image_processing/frontal_face_detector.h>
How can I resolve this issue? Why the program finds some header files but it can't find the others while they are all located in the same folder?
You need to add the dlib folder itself to the Include Directories in VC++ Directories, you instead added the folder above it.
By extension that would mean your include directive needs to be #include <image_processing/frontal_face_detector.h>.
Let me list a hypothetical example to explain better. You downloaded dlib-18.16.tar.bz2 and extracted it to c:\projects. This creates a folder named c:\projects\dlib-18.16. Within VC++ Directories you added c:\projects\dlib-18.16 to the Include Directories.
However this isn't correct, you should remove that directory and instead add c:\projects\dlib-18.16\dlib as that is the include directory for the project.
That will cause #include <type_safe_union/type_safe_union_kernel.h> to load C:\projects\dlib-18.16\dlib\type_safe_union\type_safe_union_kernel.h as well as similar internal links between files.
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.
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
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
I am using Visual Studio 2008, and I need to use certain header files from another project. I have tried to add the path in "Additional Include Directories" in C/C++ General properties pane, but my project still puts out the same errors
(fatal error C1083: Cannot open include file: 'tools/rcobject.h'.
All the other cpp and header files I am using I added as existing files from another directory, and for some headers it puts out an error and for others it doesn't. There was no change in errors after adding additional include directories.
Can someone help me, I am stuck as I need to Debug...
In the "Additional Include "Directories", did you put the path to the "tools" directory, or the path to the directory that includes the "tools" directory? It needs to be the latter.
How the preprocessor works to resolve #include directives, is to take the path specified in the #include and then append it to each of the paths specified in the "Additional Include Directories" (and some other places specific for the project). So, you need to make sure that the path specified in the "Additional Include Directories" plus the path you gave to the #include exactly matches the path to the file you are trying to include.
For example, suppose you have the following file you want to include:
c:\blah\bletch\foo\bar.txt
Then you did this:
#include "bar.txt"
Then you would need to make sure that "c:\blah\bletch\foo" was in the "Additional Include Directories".
Or if you had done this:
#include "foo\bar.txt"
Then you would need to make sure that "c:\blah\bletch" was in the "Additional Include Directories".
Enable the build log (I don't know from the top of my head where it is, shouldn't be too hard to find) and see if the paths you specify appear in the compiler command line. If not you're probably doing something wrong. Using additional include directories should just work. Just make sure you're using the right directory separator and you fill them in under the correct configuration (Release/Debug).
Regards,
Sebastiaan