Including files in C++ - c++

I am trying to include my class header in main and my class implementation. The weird thing is main finds the header file perfectly, but with the class implementation I get "no such file or directory". Why is this happenning? I mean what are the possible reasons one file in my project is able to find a header file perfectly, but another file in the same project can't find the same identical header file.
I am using CodeBlocks 10.05
Thanks

The list of paths searched by the compiler for headers must be different when compiling the class implementation.
Are the class implementation and main files in the same directory? If not then the problem could be that on most compilers the first path searched is the directory containing the file being compiled*, and the header is in the same file as the main file, but not the class implementation file.
I don't know if your compiler offers it, but some have a verbose mode that will list the paths being search for headers. If my above comment doesn't help then maybe that will.
Assuming you're including the file using quotes. The only difference between using quotes and angle brackets in your includes is that using quotes means the directory containing the .cpp file is searched for headers before all the other paths that have been listed (on the command line, in environment variables, in the implicate system include paths).

Make sure your directories are configured right, and as long as the implementation is included in the project it should compile properly. Also as Fred Larson said, make sure you put "quotes" around the file, not angled brackets, otherwise it looks in the standard directories where it won't find your file.

Related

Removing .h extension in user defined c++ header file

Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
after following up the comments and answers:
using codeblocks ide
i have created a "add" of type File and tried it including in my source file and it worked. attaching the snapshot below.
the aim of my question is to ask if userdefined header files can also omit .h extensions and how?
i am really trying to explore this fact and don't have a good understanding of how compilers stores standard header files.
A easy to understood conclusion is really appreciated
Thankyou.
Can we remove .h extensions while we define our own header file in c++?
Sure, as long as that matches the filename of the file. As far as the language is concerned, the name of the file is largely irrelevant.
However, .h or similar such as .hpp is conventional, and helps the reader of the source to understand what the file is used for. This is an important consideration.
Another consideration is that some tools use the filename as a heuristic to determine the purpose of the file. For example, your IDE might not assume that the file contains C++ code, and thus not enable C++ features such as source analysis unless you follow a common naming convention.
I have created a header file and named it add.h and tried it including in source file using #include "add" but it didn't work.i know i am missing some important concepts here.
What you're missing is that the include directive must match the name of the file. If you include "add", then you must name the file add, not add.h. If you name a file add.h, then you must include "add.h", not "add".
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
You've misunderstood how the files in the stardard library are named. The header file iostream is actually named iostream and not iostream.hpp or iostream.h (unless you use a very old compiler).
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
The reason that doesn't work is because the pre-compiler tries to read the file add and you've named the file add.h.

how can include <> ignore system default header file path(usr/include)?

I have a c++ project where the build process is organized by cmakelist, the compile stuck at a point and prompt that g++ cannot find json/json.h, I check my standard header path(/usr/include) which indeed has this hearder file. after struggle for a while I found This project use header file from its own thirdparty directory(not /usr/include) and the include_directory path was incorrectly set by me!! This upset me How can it be realized since #include <> should firstly search standard header file path(/usr/include) !!??
This documentation
https://en.cppreference.com/w/c/preprocessor/include
Says about including with ""
"Typical implementations first search the directory where the current file resides and, only if the file is not found, search the standard include directories"
I'm not certain I understand your question very well but,
if json/json.h is present in your local directories,
may be should you include this header-file with ""?

How to include header files without quotation marks?

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.

#Include doesn't see headers in others files

It's surely a noobish problem, I'm sorry for wasting your time, but I can't find any solution (I searched a bit on the internet, but found nothing).
I have a Console Application Project in Visual Studio 2013 and I have many folders where I put various .h and .cpp files. In these files I use namespaces for grouping classes.
The problem is that I can't include headers that are in parent directories in the current .cpp or .h file.
For example, I have a Hello class under the path /a/b/c, that is included in the namespace A/B/C, and a Hi class under /a/b/ that is in the namespace A/B. In my situation the Hello class doesn't "see" class Hi, but class Hi can "see" class Hello. Also, if I have Bye class in folder a/d/ with namespace A/D, class Bye doesn't "see" class Hi and Hello, and viceversa.
What I mean for "doesn't see" is that #include doesn't find the headers and that when I use "using namespace ..." there aren't parent namespaces.
P.S. I hope my English is understandable (it's not my mother language) and that what I just wrote above is clear.
In the Solution Explorer Window. Right click on your project and choose Properties from the menu.
In the C/C++ -> General settings the first field is "Additional Include Directories"
This is where you add paths for the compiler to load include files.
When the compiler encounters an #include statement, it tries to open the specified file. If the file is an absolute path, it only tries to load from that specific absolute.
If the file is a relative path, it tries to load from the directory of the file being compiled first. If the file is not found in the same folder as the c/cpp file, the compiler tries each of the paths in its 'Include Directories' list to find the file.
In visual studio, the 'Additional Include Directories' are based on the current project directory by default. You can obviously specify whatever you want using absolute paths and/or variables.
For your example you might want include directories something like:
a/b;a/b/c;a/d
I found a solution for the problem. I used as a prefix in my #include the string "../../", then I can include all my files in my directories. I don't know if this the best thing to do, but it worked.

could not understand including the header file

#include "Ser.h"
#include ".\ser.h"
Is this .\ser.h is an executable of Ser.cpp...When right clicked on it and when pressed "open the document .\ser.h" its going to the Ser.h file...
Why does they again include the executable file(./ser.h) as header file.... isnt the header file(Ser.h) enough to get all the delarations needed to be defined in.
.\ser.h is not an executable file, it's simply another header file.
In systems where file names are case-irrelevant (and this seems likely here since it's using the Windows path separator \), it may well be just including the same file twice.
If include guards are used correctly, that won't matter. If case is relevant, they're most likely two different files.
I say "may" since the order of searches for header files, and even the mapping of the header-names to headers or files, is implementation defined and xyz.h and ./xyz.h may be found in different places because of this.
There is no way of telling for sure without knowing what your include path is. This is likely just including the same header file twice on a case insensitive platform such as Windows. Which is probably a mistake but a harmless one because of include guards (those #ifdef at the beginning and end of the file).
It seems as if you are having trouble understanding the purpose of header files. There is no such thing as the header file. A header file is just the place where functions and classes are declared but usually not defined. So if ser.cpp uses some function that is defined in common_functions.cpp, you should include the apropriate header - it will usually be called common_functions.h.
Be aware that, as for almost anything in the C or C++ world, there are plenty of exceptions, but the above holds true most of the time.
The ./ser.h is not an executable file. Please keep in mind, file extensions ending in .h are generally header files.
If your platform is windows you will find executable files taking the file extension .exe.
One Ser.h file is enough to receive all the declarations, so you do not need two of the same header files.