how to call h files in c++ project - c++

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

Related

Having trouble including header files

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.

I can't include header files in my main() file

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

define cpp file location

I have a.cpp and a.h files in separate from the main project directory. I include a.h header file into main project using absolute path. Hot to tell c++ where it must look for a.cpp file?
The best way is to just add the cpp file to the project.
If it's outside the project or excluded from the build, you could include it in a another source file (#include "othercpp.cpp") and specify its path in the additional include directories under project settings.
But the correct approach would be to add it to the project. It doesn't have to be in the same place as other cpp files for this.
If your .cpp is in the project, it will find it. If it is not, you might work around that with the include directories, but thats just bad practice.
Visual studio normally compile all the cpp files included in the project,
so you don't need to 'find' it actually. Just drag it into current project.
Add the a.h to your projects default header file location. Do the same for your a.cpp. Add both the files to your project. You can now successfully build.
When I say add them to the default location, I mean the actual physical location that your main.cpp for the project is located as well as the header files for your project is located. If you put them anywhere else you will need to "Add additional include" directory paths for your header file.

Question about C++ folder structure for header file

I'm quite new in C++ after few years in Java and eclipse, I got little bit confusing using code::blocks, no autogenerate setter/getter and also implement interface :D.
I wanna ask about code structure in code::blocks, I create new console application, my header will be put to Headers/include folder called Employee.h, then .cpp will be put to src folder.
Now I create main class (I put outside of src folder) who will call the header, I just append the include code like this :
#include "Employee.h"
sure then the errors appeared after compiling:
error : Employee.h: No such file or directory.
how to link the header to the main class properly?
this is my folder structure :
updated :
It works, my include folder needs to be added on build options.
Really thanks
You need to add your include directory to your compiler's include path. This is going to be compiler-specific. e.g., if your structure is:
code
code/src
code/include
and you're running g++ from a terminal in the 'code' directory, you'd need to run (assuming your .cpp is Employee.cpp):
g++ -Iinclude src/Employee.cpp
I suspect you're running some sort of IDE, though: if so, do a search in its help for "include path" and it should tell you how to set it up correctly.
If you want to include your employee.h you must #include "employee.h" not Employee.h. Those are two different files.
You shouldn't be adding include paths to your build options for header files that are actually part of your project. It didn't find the header file from the EmployeeTest.cpp because you didn't use the full relative path.
You need:
#include "include/Employee.h"
You should only be adding include paths to your compiler for additional libraries that aren't added to the typical /usr/local/include or /usr/include directories.

Having trouble adding a dll to my project in visual C++

So I have gone through this tutorial three times:
http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx
Every time I get to the end and try to run the program, it says:
Error 1 fatal error C1083: Cannot open include file: 'MathFuncsDll.h': No such file or directory
Using a .dll was so simple in C#.
Could anyone explain to me, assuming I have a header file C:\bob.h and a corresponding dll C:\bob.dll, how I would use the functions described in the header file?
Could anyone also explain why, even if a header file is added to the header files folder with Add Existing Item, the header file cannot seem to be found?
Thank you
The 'folders' in the solution are a grouping mechanism for managin the solution, and not related to 'finding' includes or libs when compiling or linking.
If all the code isn't in the same folder, or identified using references then you may want to add include directories - with VC this is typically located under project properties/configuration properties/C C++/General/Additional Include Directories. This sets on the compiler the -I option which is to specify a path to other locations for your header files.
Say you have
C:\A.h
C:\A.cpp
C:\Project1\B.h
C:\Project1\B.cpp
And B needs to use A.
You could:
Move A files into Project 1 folder and in B.h use #include "A.h"
Change B.h to #include "../A.h"
Add addition include directories of C:\ and use #include "A.h" or #include <A.h>
Meanwhile add A.cpp and B.cpp to the project will compile them in the location they are in the file system, the object file output should all be located in the intermediate directory and usable by the linker without further issue.