Unable to include header files in C++ - c++

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 :)

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.

include<apis/api1/api.h> throws No such file or directory

#include<apis/api1/api.h>
throws No such file or directory
i even tried moving api.h and api.cc to the main project directory and using
#include<api.h>
does the same thing even though it is in the exact same directory that the other classes use
i tried adding /apis/api1 to the compiler search path
that just crashes the compiler can someone tell me what to type into the compilers compilation line
#include <api.h>
is the way you include a system header. (That is, the header for a library installed on your system.) It does not search in the directory of the source file or in directories specified in -I command line arguments.
#include "api.h"
is the way you include your own headers. (But it will also search library header locations if it doesn't find the header locally.)

Referencing a cpp/h file in different location

I have a pretty basic question. If I have a .cpp and .h file in a different location than my project, how can I reference it by saying #include " ".
I am trying to use wxMathPlot.cpp/.h and it references wxWidget cpp files.
mathplot.cpp(19): fatal error C1083: Cannot open include file: 'wx/window.h': No such file or directory
So say my wxMathPlot.cpp is located in C:\Users\Owner\Desktop and my wx/window.h is in C:\Users\Owner\Documents
#include "../Documents/wxMathPlot.h"
Should work. To elaborate:
When you use an include such as #include "header.h" the same directory as the file is searched.
When you use an include such as #include <header.h> a specific directory is searched, chosen by your compiler, which is where you will find most standard library header files.
You can reference it by using its full path or by referencing through one or more ..'s in your path (that means "go up one level"), or you can specify the directory in which the header file resides in the 'header file search path' (the 'include path') and then just use the filename.
However, using the full path is not recommended because if you move the header file relative to the file it is being referenced from, then it will not work anymore.
Look at this SO question for a nice answer as well.
For CPP files you need add those files in your project. If you are using Visual Studio you can add the cpp file by right clicking on your working project and selecting add existing item. If you want to refer a .h file you need include this, e.g
#include "../Documents/wx/Windows.h"
It is always good to use relative path rather than using absolute path.

including header files from different directories?

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

Xcode 4 C++ header file with relative path to another header

I'm using a library with an include structure where the .h files are all in a single directory. These .h files contains a single line, a #include directive which points to the 'real' header file in specific source folder locations. The #include path in these files is relative.
So, here's an example. The directory structure is:
/project
/sources
<my .cpp files>
<my .cpp files>
...
/include
/component
foo1.h
foo2.h
/platformA/something/foo1.h
/platformB/somethingelse/foo2.h
/include/component/foo1.h contains a single line of code:
#include "../platformA/something/foo1.h"
/include/component/foo2.h contains the single line of code #include "../platformB/somethingelse/foo2.h"
In my sources, I simply have:
#include "component/foo1.h"
The header search path for my project points to /include
Now, Xcode 4 is able to find component/foo1.h in /include, but it's unable to follow the relative include path within those headers and find the 'real' foo1.h in the `/platformA/something' directory, and so on.
I suspect it's because the include paths in the top-level foo1.h file is relative to its location, but Xcode might be treating it as relative to some other location (project root or something)? FWIW, Visual Studio has no problems with an identical configuration.
What can I do to remedy this?
From your directory structure it seems that the directories platformA and platformB are placed outside the include folder. There are two possible solutions to this:
Solution A
Move these
to include folder.
Solution B
Add project/platformA and
project/platformB to the
directories where include files
should be looked for in project
settings.
Don't use relative paths. Seriously, it's implementation-defined behavior how they work, so different compilers/environments/platforms will behave differently, and in your case, Xcode is almost certainly invoking GCC or clang in some sort of "build" directory, which may or may not be a sibling to your sources directory.
It's not worth the headache.
Put platformA and platformB in include, or add another directory (say, platform-include) put them in there, and add that directory to your include path.