This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Closed 7 years ago.
I have a C++ project and I love mycode organized, however when I put everything in separate directories (headers and source files kept together) I am not able to include headers from another folder.
For example: I have two classes called "FooException" and "ContentProvider", which obviously go into separate directories, FooException being an exception and ContentProvider being a utility. Of course, I put them into different folders, but when I try to include the FooException in the ContenProvider it does not work and says that it could not find the source file. Has anyone encountered such problem? Any suggestions?
Thanks in advance and Happy New Year!
UPD:
Okay, people suggested looking at the differences between #include <> and #include "" and that still did not help. Although I am able to access any files from my Main.cpp I am not able to access files neither in my ContentProvider class nor in FooException class no matter which #include-statement I use. Moreover, no matter which statement I use I can access SFML-library from any point in my project. Does it have to do with the SFML-directory being in the "include"-directory in my project?
UPD 2:
Okay, problem solved and it had nothing to do with #include <> and #include "". I just had to put "..\" before writing the name of the path and it worked beautifully. I marked the answer that suited the best right.
You have to correctly configure include header paths settings in your IDE/Makefile. To put simply, to be able search a particular header file, a compiler must know where to look for it. For example, in g++, we use -I flag to provide various include paths.
For example,
$g++ -I/usr/abc/A/ main.cpp
In main.cpp, a.h is included and aboslute path of this file is /usr/abc/A/a.h.
You can:
Add the directories which have the required header files as part of pre-processor lookup. Different compilers have different way to achieve this.
Add the header file as a relative path. For eg. #include "../folderName/A.h"
Combine 1. and 2. Add a top level directory in the search path and include files as #include "topLevelDirectory/A.h"
I personally do not like 2. because if your file hierarchy changes you will have to modify source code to change the relative paths.
Related
I'm kind of new to programming so please go easy on me. Anyways, I know about including header files that you, yourself, have defined. For example:
#include "yourHeader.h"
I'm trying to use FLTK for its GUI options, however, many of its header files include other header files using an include like this:
#include <FL/Blah.h>
instead of this:
#include "FL/Blah.h"
I would have to go every header file that has the include in angle brackets and change them to quotation marks for them to work. I am currently working in CodeBlocks right now, if that matters. Is there any way to include the header files using angle brackets instead of quotation marks, or am I stuck with having to go into the header files themselves and manually swapping them all out?
Generally, the header file from
#include "headerfile"
will searched in the current source path. If the search fails, it is reprocessed as if
#include <header file>
does.
Your FLTK library is using include like the following?
#include <FL/Blah.h>
The FL's parent path should be in the predefined INCLUDE path. You may edit you Makefile or project settings.
You can add the folder which contains all your headers to your include path while compiling.
How to add a default include path for GCC in Linux?
Some background
Ok there are two set of include search path.
The user include path:
This is usually only the current directory (also known as ".").
Note: It may be others but for simplicity lets just use "." in the examples below.
Then there is the system include path:
This is usually a few places in your machines (could be /usr/include and /usr/local/include).
Note: It may be others but for simplicity lets just assume these in the examples below.
How it usually works.
There are caveats and not all compilers work exactly the same. But the following are a good rules of thumb.
When you include a file using quotes "".
#include "yourHeader.h"
It will search for this file in all the directories specified in the "user include path". If it fails to find them there it will then look in all the directories specified by the "system include path". So your compiler will search for the following files:
./yourHeader.h
/usr/include/yourHeader.h
/usr/local/include/yourHeader.h
It will use the first one it finds.
When you use the <> in the include:
#include <FL/Blah.h>
It will search for the files in the "system include path" first. Then depending on your compiler may optionally search the "user include path" (but lets assume not for now).
So in this case it will search for the files:
/usr/include/FL/Blah.h
/usr/local/include/FL/Blah.h
It will use the first one it finds.
Modifying the Default
So these are the default paths that will be searched for a file. But your compiler will allow you to add extra paths to both of these search paths (usually). It depends on your compiler how to add search paths.
For gcc (and probably clang) it uses -I and -isystem (and probably more)
Expectations.
When you see <> in the include header it usually means this is an already installed library that you are looking for. So your code assumes that the FLTK library has already been installed on your machine.
When you see "" in the include header you should assume that it is a local file that belongs to the project.
I want to add an #include directive to my A.cpp file, and the header file B.h is in the folder two layers up (for example, if the source file is in *E:\A\B\C\D\E\F\G\H*, the header file is in *E:\A\B\C\F*, of course, the names will be much longer than this), so I typed this confusing statement in Visual Studio 2010:
#include"../../
And the Intellisense feature in VS 10 showed up a list of the files, and B.h is in it! I don't know if this statement is correct, but I think there is something wrong with this. Could any of you tell me whether this is wrong or correct? And could you give me a better solution for this?
Thank you very much.
Why wouldn't it work? Have you ever done cd ../../mydir and checked what that does? It's the equivalent of that. .. and . point to a special directory in both Windows and Linux. . is the current directory while .. is the previous directory. So ../../file.h would go two directories back to find the file you're looking for. After all, the #include "..." statement works with the relative path the file is located in. Usually when writing libraries and you have a main detail subdirectory you would try to access it via #include "../detail/myfile.hpp", while other libraries such as boost opt for #include <boost/config/myfile.hpp>
I think it is best to include the "-I <INCLUDE_DIR>" parameter to your build and just #include the header without the directory listed. The is where you are telling the compiler and linker to look for your header files.
..\ moves you up one directory, so ..\..\ will move you up two directories. I suppose this is fine if you can assume the script location will never change. If, however, you moved the script to a different drive you would be in trouble. It might be better to include E:\A\B\C\D\E\F\B.h instead to future proof yourself.
I'm facing difficulty in understanding source and header files stuff.
Suppose
1)I have a source file(functions.cpp) which contains function named 'int add(int x,int y)' in the location /Users/xyz/Desktop/functions.cpp.
2)The header file(functions.h) which contain the declaration of the functions in source file(functions.cpp) is placed in /Users/xyz/Documents/function.h
3)Other source file(main.cpp) which contain 'main()' function need to call the 'add()' function defined in 'functions.cpp'.The source file 'main.cpp' is located in /Users/xyz/Downloads/main.cpp
I'm placing these files in different locations so that i can understand these concepts better.
So,how do i link function.cpp to main.cpp using functions.h.
#include " "
What is the path that i should use in the above include?
Also,it is my understanding that .h files provides the declaration of functions which are defined some where else and having a declaration is necessary for the compiler to call the functions which are defined in some other files or functions which are not defined yet. Is that right? Please correct me in case I'm wrong.
#include "functions.h"
Your code should not know about how you choose to arrange your source tree. To hard-code paths is to earn the hatred of whoever has to maintain this code (and that includes you six months from now).
Your build system -- whatever it is -- can deal with the paths. That could be as simple as:
g++ -I/Users/xyz/Documents -c functions.cpp
Your statement of how declarations/definitions work is basically correct.
Your first question has no answer. C++ does not define how header files are found, it's up to the compiler and they all do it a bit differently. If you want an answer you'll have to look up the details in the documentation of your compiler. I would recommend you put everything the same directory and stop worrying about it.
In the second part of your question, your understanding seems pretty good to me.
You should include in your main the exact path to your header file:
#include "/Users/xyz/Documents/function.h"
Hope this help.
Regards.
You include functions.h in functions.cpp and main.cpp using #include then you compile both main.cpp and functions.cpp. The linker then links the two resulting object files. Your inclusion of functions.h in main.cpp will allow you to call the functions from functions.h within your main.cpp file
As for paths of files, provided that you specify to your compiler the required paths in which to find your code you should be fine.
You can either use a full path
#include "/Users/xyz/Documents/function.h"
or a relative path (which is usually more preferable)
#include "../Documents/function.h"
Don't forget to specify full or relative paths to your .obj files when you are linking the final executable as well ;)
For example, if I had a file called foo.h, can I always just include it by doing:
#include "foo.h"
or do I sometimes have to do something like:
#include "bar/foobar/foo.h"
EDIT - are they simply used to make the compile time shorter by limiting the search for the file?
Basically, you can pass include paths as options to the C++ compiler, but it won't look for the file recursively. If you pass in the path /opt/include and do "#include "foo.h", it will _ only_ find /opt/include/foo.h, not /opt/include/dummy/foo.h.
In other words, either you pass every possible include path on the command line, or pass the root in and "navigate" by using the #include "dummy/foo.h"
Edit: #MatthieuM. made another good point below, #include "mylibrary/api.h"makes it much clearer which include file you're using than just #include "api.h", especially if you're using multiple libraries.
The compiler isn't going to recursively search your source directory. If you want to use #include "foo.h", you'll need to compile with -Ibar/foobar to tell the compiler it should look in bar/foobar for header files.
Otherwise, you'll have to use the full (relative) path. I tend to prefer this over -I, because it makes the source more independent from compiler flags (which you'll appreciate when changing between build systems).
There may be cases where there are multiple header files with the same name - in this case, a path helps to disambiguate.
I think it depends of your project settings, for example you have section like "Additional Include Directories" in Visual C++ so IDE will also search files in these directories.
The first statement #include "foo.h" will search for foo.h in the current working directory or in the directories mentioned under additional include directories.
If your header file is in some other path you need to explicitly mention it.
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.