I have an external dependency in my project which I can't change. It is built with some very complex Python which calls some more build tools.
This all eventually boils down to compile error: the C++ just cant find a header file.
The header file is included like this:
#include <sqlite3.h>
The question:
Is it possible to add a custom include directory to Visual Studio C++ with the help of just environment variables. And if it is - how?
I can't change the solution nor project nor source files - they all are being download automatically
I'm using most recent visual studio - 2017
Is it possible to add a custom include directory to Visual Studio C++
with the help of just environment variables.
I'm afraid the answer is negative.It's not possible to achieve this goal only with Environment Variable.
Please check this: When compiling the project in VS, if the compiler encounters an #include statement, it tries to open the specified file. If the file is an absolute path, it only tries to load from that specific path.
If the file is a relative path, it tries to load from the directory of the file being compiled first. If the file is not found in the same folder as the cpp file, the compiler tries each of the paths in its 'Include Directories' list to find the file.
And in VS IDE, we can set the Include Directories or Additional Include Directories to set the search path.So if I have a header file Test.h, and I use statement like #include <AbsolutePath\Test.h>, the compiler can find the header.
If i use statement like #include <Test.h>, the header file can't be found until I set the search path. In this situation, I can set the Additional Include Directories for all both debug and release to make the compiler find the header file (Test.h locates in Company folder):
Note: But one point we should know is most of the time, when we change the settings in Project=>Properties we actually is modifying the project file.(xx.vcxproj).
As you mentioned above, we can't make any change to project files, so
we can't achieve the goal by this way. Instead i think you can try
using
Directory.build.props
file.
Create a Directory.Build.props file, move it to root directory of your project folder. For me: C:\Users\userName\source\repos folder. Add the content like below into it:
<Project>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>C:\Company;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
</Project>
We can put the header file into the C:\Company(We can specify our folder in this way) folder and the compiler can find it. Also, we're not doing any change to Solution, project or source file.
Please let me know if it's helpful:) Any feedback feel free to contact me.
Related
I have downloaded and tried to build The oxygine engine project with VS 2017 Community. The stringutils.cpp file from it gives me the following when I try to compile:
C1083: Cannot open include file: 'SDL_stdinc.h': No such file or directory
hovering the #include statement it says cannot open source file 'SDL_stdinc.h'
I checked
Properties->C/C++->General->Additional Include Directories
and it references the path the header files are in. I have even added another path representing the absolute path. I tried a different folder, a different download of the header files, all to no avail.
What do I need to do to get this right?
Is there any reason for a header to not work? (version mismatch or something)
Is a header not working if the corresponding dll or lib is missing?
If so, how do I know it is found by VS?
The problem was the path for oxygine in the oxigine project properties was setup like this ..\..\..\..\SDL\include\ but had to be setup like that ..\..\SDL\include\
I was trying to re-use an available source code for my own project, it can be found here:
https://github.com/TadasBaltrusaitis/OpenFace
I tried compiling project FeatureExtraction of the original code, everything was fine. Then I created a new empty project and added the following #include:
#include "LandmarkCoreIncludes.h"
#include <Face_utils.h>
#include <FaceAnalyser.h>
#include <GazeEstimation.h>
These are exactly the same as in project FeatureExtraction in the provided source code. I've already changed the additional include directories in C/C++ general tab into:
$(SolutionDir)\lib\local\FaceAnalyser\include
$(SolutionDir)\lib\local\LandmarkDetector\include
However, it still gave me "cannot open source file error".
Update: If I put the absolute path of the header file directly to the code it is OK, however if I put the absolute path to the Additional Include Directories, the error remained.
Use #include "header.h" instead of the one with diamonds (< and >) which looks in another directory.
After that, check if the header files really are in these directories. If they are, you should check the $(SolutionDir) ( I don't use a '\' after the $(SolutionDir) but it may work out as well).
Try to locate and delete the .suo file and restart VS
Looks like I had same "bug" as mentioned in this post here:
Visual Studio does not honor include directories
After having changed the Additional Include Directories for all platforms instead, the code was compiled without any errors.
could you please help me out by answering my following question about the Visual Studio include behavior?
I create a new C++ Project, which references an already existing one, whose source files should not be altered. I end up with the following file
structure:
-MyProject
+---MyProject.sln
+---MyProject.vcxproj
+---MyProject.cpp
+---CommonHeader.h
-ExistingProject
+---project
+---+---ExistingProject.vcxproj
+---source
+---+---ExistingProject1.h
+---+---ExistingProject2.h
+---+---ExistingProject1.c
+---+---ExistingProject2.c
At the beginning, i could not even compile the ExistingProject. Within the ExistingProject1.c, the following include statement exists:
#include <ExistingProject1.h>
From here i learned, that in case of the bracket-includes, Visual Studio does not look for the Header within the same directory as the file that contains the include statement. So my first question would be:
1) Is it true, that i have to add the "../source/"-Directory to the Include Directories of the ExistingProject.vcxproj, although the Header files are already added to the visual Studio project?
To use the structures and functions of the ExistingProject, i have to include the ExistingProject2.h into my MyProject.cpp. But within the ExistingProject2.h, the same include Statement
#include <ExistingProject1.h>
exists. This leads to my second question:
2) Is it true, that i have to add the "../ExistingProject/source/-Directory to the Include Directories of MyProject.vcxproj project as well?
And now comes the really strange problem. If a certain define is set, which has to be set within MyProject, the ExistingProject2.h also includes an external header file via
#include <CommonHeader.h>
Which has to be defined by the dependent project. This leads to my last question:
3) Is it true, that i have only the following two options to compile with this external header file?
I have to copy my CommonHeader.h into the "ExistingProject\source\"-Directory during checkout.
I have to add the "."-Directory to the Include Directories of MyProject.vcxproj project and i have to add the "../../MyProject/"-Directory to the Include Directories of ExistingProject.vcxproj.
The second option just makes no sense. Is the first one really my only option to deal with this bracket-form include of an external header file?
And: Shouldn't be at least the location, next to the vcxproj file, be within the include directories of a header file, which is used within that project?
Sorry for the stupid spelling. I am a really bad explainer.
Wish you all a nice weekend.
When you put #include <header.h> - Visual Studio will search for it in all specified include paths in project settings, this includes default paths, where Windows SDK and standard library resides. See this screenshot, as example:
If you put C:\path1\path2 to include directory, you might end up using #include <..\path1.h>
In case of #include "quotes.h" - Visual studio will search for files only in project directory, where you store your files. If you put additional files in some sub-folders in project - you always need to specify relative path (like #include "subfolder\header.h").
Let me know, if you need further clarification on this.
This is the exact definition of how Visual Studio uses bracket includes (<>) versus quote includes (""): https://learn.microsoft.com/en-us/cpp/preprocessor/hash-include-directive-c-cpp .
Summary: Brackets <> will stick strictly to the "include directories" settings. Quotes "" will also do "include directories", but will first search local project directories.
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.
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.