On Visual Studio I see one can set path of i.e. util\util.h on that side panel, right-click the project then
Properties > C/C++ > General > ...
I add util directory there and then I can do #include "util.h" on a file from another directory, instead of providing the full path util\util.h.
Besides the approach descrived, is there another way which also will allow me to #include "util.h" (not provide the full path)?
I'm not familiar with Visual Studio at all.
Within the the Visual Studio Dialog you describe under: Properties > C/C++ > General > if you select the drop down icon and select "Edit" you will see a button titled "Macros" this provides a list of available Macros for use on the "Additional Include Directories" line. Likely you will want to use: "$(ProjectDir)\util". $(ProjectDir) is the directory that your project file is sitting in.
Let say you have created a project names 'Project1'. Then there will be a 'Project1.vcxproj' file which stores the settings for the project. Visual studio also looks for any files (source or header) in the folder starting with where this project file is located. Use the relative path with respect to where this file is located.
Let say your directory structure is as follows:
-Project\
--Project1\
---Project1.vcxproj
-Source\
--header.h
Use include path as
#include "..\Source\header.h"
Related
I am using Visual Studio 2017 and I'm required to use DirectX9.0 for a school project.
I have a solution with 3 projects. One is a static library (.lib), one is an application (.exe) and one is a unit test project:
KB01_Game.exe imports KB01_Engine.lib like so:
KB01_Game properties
KB01_Engine.lib contains the d3dx9 references like so (I have also tried reversing the order of these DXSDK references, with no success):
KB01_Engine properties
The files in KB01_Engine include the directx 9 headers like this:
> #include <d3d9.h>
> #include <d3dx9.h>
> #include <dinput.h>
I have tried the quotation marks too, with no success
> #include "d3d9.h"
> #include "d3dx9.h"
> #include "dinput.h"
KB01_Engine builds successfully. I get no errors saying that there are include files missing. But as soon as I build KB01_Game it gives me this error that is also included in the title, that I have struggled months to fix...
The external dependencies/header files for directx appear in KB01_Engine as expected. When I right click on d3dx9.h and click the option "Open document 'd3dx9.h' " it actually opens the correct document. There are no indications that it can't find the file. I'm strongly assuming that this build error happens because I am using KB01 engine as a static library and not as a regular application.
If you would like to view the code, it is available here: https://github.com/bdeboer95/KB01-Revamped
I fixed it thanks to paisanco's comment by adding these two things:
1) Apparently it is needed to add Library directories too, which I missed in KB01_Engine properties. "Librarian > Additional Library directories"
2) And then I added the include directories for DirectX in KB01_Game properties "C/C++ > Additional Include Directories":
I previously had all of my source files in the same folder just scattered about, but I was getting tired of the disorder, so I created a folder hierarchy and organized my headers and .cpp files in those folders, and changed the ClInclude tags in the projects .vcxproj file accordingly. I then changed all the #include rows in the source headers to match the new location of the headers.
Now when I compile I get a heap of errors saying that the namespace "math" can't be found. The namespace could be found before the file organizing, so the problem should be something with that, but that #includes works fine.
If I write
using namespace
the autocomplete finds the namespace just fine, and no red wiggly lines show up in the text interface.
I'm using VS17 Community.
There are actually two ways to resolve your issue. You've already noted the first, that you can always use relative paths (relative to the source file).
#include "..\Math\math.h"
This will work just fine. You can also modify your C++ Project Properties to add a list of include directories. This will be very useful for you when you begin working on projects that use a lot of libraries To add include directories you need to:
right-click on your project in the solution explorer and select Properties.
In the left pane, click on the C/C++ > General tab.
On the property that says Additional Include Directories, click the drop down arrow (you may need to click inside the text first) and select Edit...
From here, you can add a list of include directories that you will be using with your project. You can use:
Absolute Paths
C:\Path\to\Math
Relative Paths (relative to your .vcxproj file)
..\..\relative\path\to\Math
Macros (Visual Studio will list your available macros)
($SolutionDir)..\path\to\Math
You'll see the use of the macros a lot when it comes to C++ and other Visual Studio projects not just for include directories, but for build events as well.
Once you've added a list of include directories, you can go back to including your headers in the source code as normal:
#include "math.h"
In the event you need to use a relative path from one of your included folders, you can do that as well.
// some directory inside of Math
#include "MoreMath\moremath.h"
// some directory above Math
#include "..\AboveMath\abovemath.h"
Hope this helps!
I solved it...
Remove this question if necessary. The problem was that I didn't backtrack the filepath in my #include "math.h". They should instead have said #include "..\Math\math.h".
I am using Visual Studio 2012 and trying to import header files in the project, but have been failing miserably. Here is what I have tried to do:
#include "gevents.h"
#include "gobjects.h"
#include "gwindow.h"
int main(){
int x=1;
return 0;
}
I have added these three header files in the project location:
C:\Users\Shaby\Documents\Visual Studio 2012\Projects\ConsoleApplication39
In addition, I have also gone to Project properties ->Configuration Propertues -> C/C++ ->General and included the above path location in "Additional Include Directories" but this had yielded nothing. Instead, I am getting the following error:
Unable to start program C:\Users\Shaby\Documents\Visual Studio 2012\Projects\ConsoleApplication39\Debug\ConsoleApplication39.exe The system cannot find the file specified
#Usman Khan ,
I have one solution for your problem.In Solution Explorer Window (if not opened then press Ctrl+Alt+l) you can see you project name.Right click on it and than Add > Existing Item. Now select your header files which you want to include & press Add. Done :) .
why are you making it complex, just keep it simple.After creating your project. Open solution explorer. Under your project name you would see a folder "header files" , add your header files in it (your header files should have ".h" extension) and then to use them include them in your source 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 am working on a game using Visual C++. I have some components in separate projects, and have set the project dependencies. How do I #include a header file from a different project? I have no idea how to use classes from one project in another.
Settings for compiler
In the project where you want to #include the header file from another project, you will need to add the path of the header file into the Additional Include Directories section in the project configuration.
To access the project configuration:
Right-click on the project, and select Properties.
Select Configuration Properties->C/C++->General.
Set the path under Additional Include Directories.
How to include
To include the header file, simply write the following in your code:
#include "filename.h"
Note that you don't need to specify the path here, because you include the directory in the Additional Include Directories already, so Visual Studio will know where to look for it.
If you don't want to add every header file location in the project settings, you could just include a directory up to a point, and then #include relative to that point:
// In project settings
Additional Include Directories ..\..\libroot
// In code
#include "lib1/lib1.h" // path is relative to libroot
#include "lib2/lib2.h" // path is relative to libroot
Setting for linker
If using static libraries (i.e. .lib file), you will also need to add the library to the linker input, so that at linkage time the symbols can be linked against (otherwise you'll get an unresolved symbol):
Right-click on the project, and select Properties.
Select Configuration Properties->Linker->Input
Enter the library under Additional Dependencies.
Since both projects are under the same solution, there's a simpler way for the include files and linker as described in https://learn.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=vs-2019 :
The include can be written in a relative path (E.g. #include "../libProject/libHeader.h").
For the linker, right click on "References", Click on Add Reference, and choose the other project.
Expanding on #Benav's answer, my preferred approach is to:
Add the solution directory to your include paths:
right click on your project in the Solution Explorer
select Properties
select All Configurations and All Platforms from the drop-downs
select C/C++ > General
add $(SolutionDir) to the Additional Include Directories
Add references to each project you want to use:
right click on your project's References in the Solution Explorer
select Add Reference...
select the project(s) you want to refer to
Now you can include headers from your referenced projects like so:
#include "OtherProject/Header.h"
Notes:
This assumes that your solution file is stored one folder up from each of your projects, which is the default organization when creating projects with Visual Studio.
You could now include any file from a path relative to the solution folder, which may not be desirable but for the simplicity of the approach I'm ok with this.
Step 2 isn't necessary for #includes, but it sets the correct build dependencies, which you probably want.
#include has nothing to do with projects - it just tells the preprocessor "put the contents of the header file here". If you give it a path that points to the correct location (can be a relative path, like ../your_file.h) it will be included correctly.
You will, however, have to learn about libraries (static/dynamic libraries) in order to make such projects link properly - but that's another question.
You need to set the path to the headers in the project properties so the compiler looks there when trying to find the header file(s). I can't remember the exact location, but look though the Project properties and you should see it.
Try to avoid complete path references in the #include directive, whether they are absolute or relative. Instead, add the location of the other project's include folder in your project settings. Use only subfolders in path references when necessary. That way, it is easier to move things around without having to update your code.