So I have the following file structure:
Graphics (folder)
RenderCore.h
RenderCore.cpp
MainProject (Folder)
app.h
app.cpp
Water (Folder)
WaterRenderer.h
WaterRenderer.cpp
I can use #include <Graphics/RenderCore.h> from WaterRenderer.h, or from any other header file in any other folder in the project. Except for app.h.
For some reason, when I try to use #include <Graphics/RenderCore.h> or #include <RenderCore.h>, I get an error message, telling that the file doesn't exist.
The same goes for any other header file in any other folder, if I try to import it into app.h.
But, if I move app.h and app.cpp into a different folder, I can use the above-mentioned includes, but I can't start the program, because it asks me to "Select a valid startup file".
Does anyone know a possible solution or at least a cause? I'm using VisualStudio 2019 on Windows 10.
There are two methods to include header files.
Add the full path directly to the head of the source program
#include "D:\testfolder\Graphics\RenderCore.h"
Properties->VC++->Include Directories->add the path
Note: Solution Configuration settings need to be consistent with the settings on the Properties
Related
I can only use header files that I add to C:\SDL2-w64\include, but I Can't get it to link to the include folder inside my project folder.
for example: I have a folder called "MyProject" and its located at C:\Users\User\Desktop\MyProject, inside the project folder there is an "Include" folder located at \MyProject\Include then I add a header file called "head.h" to the Include path. in my code I use #include <head.h>, I then get the error cannot open source file "head.h", but if I add head.h to C:\SDL2-w64\include it works perfectly fine.
anyone know how to include the Include folder located in my project folder?
Assuming the file doing the including is located at C:\Users\User\Desktop\MyProject, try using #include "Include/head.h". This is because there is a difference between using quotations ("") and angle brackets (<>) in your includes.
The reason is explained in this answer: https://stackoverflow.com/a/3162067/19099501
But to summarize it, with most compilers, #include "blah.h" will search in the local directory first, and then the stuff it can reach from what's in your PATH variable, and #include <blah.h> will search from what's in PATH only. I am guessing that C:\SDL2-w64\include is in your PATH. That is probably why when you moved the header file to that location, it was able to find it.
So there are 1 files that I want to link together, Core.h and Events.h.
Core.h is in a folder called DevEngine and the Events.h file is in a folder called Events witch is inside DevEngine.
Here are the file directorys:
Core.h = src/DevEngine/Core.h
Events.h = src/DevEngine/Events/Events.h
I have added a #include "DevEngine/Core.h" : Cannot open include file: 'DevEngine/Core.h': No such file or directory DevEngine. I don't know where I have went wrong.
I have tried: #include "../DevEngine/Core.h". That still gives me a error.
You can do #include "../Core.h".
You can also set the directories the compiler uses to search for the header files (-I option in gcc) and then use paths to those files relative to one of those directories. (See for example gcc documentation on search paths.)
This could be done differently depending on the way you are building the project.
For Visual Studio, look in this thread.
For CMake, use include_directories.
In netbeans I am creating a new folder and adding header files to it.
Now when I include the header file within the newly created folder to another file by using:
#include "folder1/myheaderFile.h"
The compiler complains that it is unable to find the header file.
The error is:
main.cpp:31:39: fatal error: folder1/myheaderFile.h: No such file or directory
Is there some way out as I want to include the header files within a folder in my #include?
EDIT: Do i need to make a makefile for every folder?
Another EDIT:
When I right clicked on the error its showing
unresolved directive
#include
Analyzed system include paths:
/usr/include/C++/4.6
/usr/include/C++/4.6/x84_64_linux_gnu
/usr/include/C++/4.6/backward
/usr/lib/gnu/x86_64-linux-gnu/4.6/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu
/usr/include
Here's for your convenience:
The include file paths you have specified are for system-wide headers. Is the header you are including yours or downloaded/installed system-wide? Do you see the path of the header in the output?
If you are including the header which is in a folder, from another folder, then you need to traverse back, i.e: #include "../folder/header.h"
If this is a system folder, such as the ones residing in /usr/local/include in my system, all you have to do is
#include <header.h>
or if it resides in a sub-folder (quite often),
#include <Libname/header.h>
As long as you have set the include paths pointing at it, it should work.
To setup the include paths and directories, see example: http://zetcode.com/articles/netbeanscdevelopment/ near the end of the page.
Remember that when you hardcode paths, you need to take into consideration the current path of the file which is including the header.
Alternatively, you can use cmake & make (don't know what Netbeans uses), where you define everything your self.
You can test with full path, i.e:
#include "/home/user/project/folder/header.h
or you can test from command line and set the include path.
Hope it helps :)
I am working on a project and I keep getting stumped on how I am supposed to import files from a different directory. Here is how some of my files are organized:
-stdafx.h
-core/
-->renderer.cpp
-shapes/
-->sphere.h
-->sphere.cpp
how can i access the stdafx.h and shapes/sphere.h from the core/renderer.cpp?
There are many ways. You can #include "../stdafx.h", for instance. More common is to add the root of your project to the include path and use #include "shapes/sphere.h". Or have a separate directory with headers in include path.
One (bad) way to do this is to include a relative path to the header file you want to include as part of the #include line. For example:
#include "headers/myHeader.h"
#include "../moreHeaders/myOtherHeader.h"
The downside of this approach is that it requires you to reflect your directory structure in your code. If you ever update your directory structure, your code won’t work any more.
A better method is to tell your compiler or IDE that you have a bunch of header files in some other location, so that it will look there when it can’t find them in the current directory. This can generally be done by setting an “include path” or “search directory” in your IDE project settings.
For Visual Studio, you can right click on your project in the Solution Explorer, and choose “Properties”, then the “VC++ Directories” tab. From here, you will see a line called “Include Directories”. Add your include directories there.
For Code::Blocks, go to the Project menu and select “Build Options”, then the “Search directories” tab. Add your include directories there.
For g++, you can use the -I option to specify an alternate include directory.
g++ -o main -I /source/includes main.cpp
The nice thing about this approach is that if you ever change your directory structure, you only have to change a single compiler or IDE setting instead of every code file.
You can either use relative paths:
#include "../stdafx.h"
#include "../shapes/sphere.h"
or add your project directory to your compiler include path and reference them like normal:
#include "stdafx.h"
#include "shapes/sphere.h"
You can use the /I command line option to add the path or set the path in your project settings.
You can use g++ -I /source_path /path_of_cpp to compile. /source_path is the path of the header file. Additionally, you can include the directory in which the header file is located in CPATH.
You can locate the header file by entering the following in the terminal locate header.h. The folder thus obtained can be extorted to CPATH using export CPATH = /source_directory. Replace /source_directory with the path of the directory obtained from locate header.h
first i created a folder name 'C:MyProject' and then another 2 folders inside the 'MyProject' have created called 'src' and 'include'.After that i opened a new project in c++ and saved it in the 'MyProject' as myproject.dev. then i added 2 folders to my project for that i used same name which i created in my project folder('src','include'). then two headear files i added to the project name a.hpp and b.hpp (inside include folder) and also main file added to the src folder. but when i compiler it shows a error that 'a.hpp:no such file or directory in function main()'. however i wrote a code to read header files from main file ( #include "a.hpp" ) and in my a.hpp, i wrote same code to call b.hpp file.
i tried several ways but it shows same error message. so please help me to create a project with header files. thanks
When the prepocessor reads #include "a.hpp" it looks for a.hpp in the current folder. In your case, it means src. But your header files are not there ! They are in C:/MyProjects/include.
You need to tell the compiler where to look for include files. Usually it's something called "include directories" in the project options.
#include "../include/a.hpp"
or add that folder to project include path