How to include all header files with one include in c++ project? - c++

I have a folder containing header (.h) and c++ (.cpp) files.
Folder/ --- file1.h
--- file1.cpp
--- file2.h
--- ...
I have a main.cpp file located in the same directory as the Folder or project (to become library).
// main.cpp
#include "./Folder/pointtoallheaders.h"
How can I include one header file that then includes all of the header files associated with the project?

Unless I'm missing somenthing it's quite trivial
You can have a header file in the same folder of the main.cpp that includes all the others:
For instance allheaders.h
#include "Folder/header1.h"
#include "Folder/header2.h"
#include "Folder/header3.h"
#include "Folder/header4.h"
Then in your main function just include allheaders.h
#include "allheaders.h"
int main(){
}

Create one header file that will import all header files in your project and then import this specific file from the cpp file

Using cmd in windows you can create single header file. this new header file should include all your old header file. Just add the new allheader.h into the main program.
FOR %i IN (*.h) DO echo #include "%i">>allheader.h
if you have folder path to add, then the above code can modify with folder name
FOR %i IN (*.h) DO echo #include "FolderName/%i">>allheader.h
Further you can create a bat file for the same process just copy and paste the above command into text file and rename with .bat extension.

Related

If I have a directory of cpp files how can I include them all?

So I'm creating a project with this kind of structure:
ProjectFolder
-SubFolder
-File1.cpp
-File2.cpp
-File3.cpp
-File4.cpp
-Header.h
-Header.cpp
-main.cpp
Is there any way that in main.cpp I can #include "SubFolder", such that File1.cpp, File2.cpp, File3.cpp, and File4.cpp are all included, without having to write a #include for every file (in case the name changes or I add another file to SubFolder later?
Yes, including all of them is necessary, as main.cpp gets user input to choose which file to run

Cmake include path for system and program files

I'm wondering if there is a possibility to configure cmake, that it's possible to make a difference if headerfiles are included with #include<...> or #include "..."?
Lets assume that I have a project like this:
src
foo.h
foo.c
lib
foo.h
foo.c
How can be achieved to add the file src/foo.h by writting #include "foo.h" and if the headerfile is included with #include <foo.h> the file at location lib/foo.h is loaded?
It always load the files from the first include_directories() command
include_directories(src)
include_directories(lib)
EDIT
My current problem inside my project:
I have a file named string.h. Which contains some function for custom text handling. Now I have the problem that if I write #include <string.h> my custom file is loaded, but I expect the system file string.h. I don't want to rename my custom file nor I want to move it inside a folder to include it like #include "custum/string.h"
All I want is:
#include <string.h> // load file from system library
#include "string.h" // load my custom file
I used it inside IDE Keil like this ways, but I don't know if it can be done with cmake
Change include order
include_directories(lib)
include_directories(src)
You will need to place your whole code in the src. In this case #include "foo.h" starts search files in the same directory where current file is parsed. #include <foo.h>" starts search in include directories in the order of include_directories.
BTW it is a bad practice to do such things. Your files should be searched first and then lib files. Better way is to do like
include_directories(src)
include_directories(parent_of_lib)
Use #include "foo.h" or #include <foo.h> for your own included files and #include <lib/foo.h> for third-party included files.

no such file or directory for .h file error in Qt after organizing project

consider that i have a main.cpp file in root which include "mainview.h" in it.
The mainview.h file is in root/View.
I have configured the .pro file(add View/mainview.h to HEADERS tag) but it still can't find mainview header in main.cpp
If you do e.g.
#include "mainview.h"
then the preprocessor will look first in the same directory as the source file doing the #include, then it will look in the system directories. If you have a header file in a sub-directory then you need to specify that too:
#include "View/mainview.h"

Can't open header file in C++

I have a header file called simpio.h, which is in the same folder as the file which is including it in its header. However, I keep on getting the error "Cannot open include file: 'simpio.h': No such file or directory." I am using Visual C++ 2008 Express Edition. Help would be appreciated.
Thanks
You need to use double quotes:
#include "simpio.h"
You have to know that you should use <> when you are trying to include a standard library header or when you want to include a file for which the path is contained in the Additional include directories option.
You have to use "" when you whant to include a file who doesn't satified the previous explanation, let's say that it is almost always file specific to your project.
Some example :
#include <iostream> // library file
#include <boost/thread.hpp> // there is "C:\SDKS\boost in my Additional include directories
#include "MyHeader.h" // Local file, at the same place of my .vcproj
#include "Header/AnotherHeader.h" // Local file in a folder named "Header"
In your case, we can think that you are in the second case. You just have to do :
#include "simpio.h"
Or if your file is in another folder :
#include "SomeFolders/simpio.h"

how to call h files in c++ project

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