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.
Related
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 am trying to include a few libraries in code blocks, however when I add the path of the .h files to the search directory (example C:\Qt\5.1.1\mingw48_32\include\QtNetwork), it only seems to identify the ones in the main file and I think that is due to the fact that in the main file they are included as such (for example) #include "qtcpsocket.h", whereas in the .h file they are included as (for example) #include <QtNetwork/qabstractsocket.h>.
Apart from the fact that one includes the folder in which it is located, what is the major difference? Why it may not work? and what do it need to do to change it?
one more thing I'm sure the files are in the folder.
Here are a few code snippets if this helps
location of file
error
If your source code contains, e.g.
#include <QtNetwork/qabstractsocket.h>
then you are requesting the preprocessor to find and include a file called
QtNetwork/qabstractsocket.h
(or, QtNetwork\qabstractsocket.h, if you're on Windows, as you are)
in one of its search directories.
And if you have specified compiler search directories:
C:\Qt\5.5.1\mingw48_32\include\QtNetwork
C:\Qt\5.5.1\mingw48_32\include\QtCore
then the preprocessor will search the first directory for:
C:\Qt\5.5.1\mingw48_32\include\QtNetwork\QtNetwork\qabstractsocket.h
which does not exist. And it will search the second directory for:
C:\Qt\5.5.1\mingw48_32\include\QtCore\QtNetwork\qabstractsocket.h
which does not exist either.
The usual way would be to specify the compiler search directory:
C:\Qt\5.5.1\mingw48_32\include
and in your code write your #include <...> directives like:
#include <QtNetwork/...>
#include <QtCore/...>
I've been searching for how to do this for nearly an entire day.
At first, I thought that this can be done by...
Right click the project name -> "Add files" -> choosing a .cpp file which contains the class you need and the corresponding header file.
Then, the .cpp file and header file appeared with its original folder. After this, I wrote #include"random.h" on the project which needed to use random.h and its functions.
However, this produces an error, indicating that fatal error: random.h: No such file or directory. The compiler apparently can't find the file (even though I can).
I add a picture of this.
Also, I've been looking for how to add .cpp & header files without its folder.
(In the picture above, for example, you'll see that random.cpp inside Using_a_class_test is included in a folder named Random. To my shame, I haven't found how to eliminate such a folder.)
I'd appreciate if you'd offer any insight.
Unfortunately, what you did is not enough. When you try to compile
#include "random.h"
The compiler needs to know where the random.h file is, and for that it uses the include path info, which is unrelated to the files you included in the project.
A couple of solutions:
You modify all occurrences of #include "random.h" to be #include "/path/to/random.h"
You modify the include path information for your project. Go to Project >> Build options, select the tab "Search directories" and add all the paths to your .h files there.
Hope this helps.
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 :)
I have the following directory structure:
I wrote the includes:
#include "obj.h"
#include "textura.h"
Yet, I'm getting:
fatal error C1083: Cannot open include file: 'obj.h': No such file or directory.
Why?
I tried previously to move the files to "header files", it didn't work, same error.
Take a look at the directory path of obj.h (should be in file properties) and make sure that the directory that it's in is present in your project's default include directories.
"Directory structure" that is shown does not actually exist. It is neither directory nor structure. Folders inside solution explorer are used only to visually group files, and for nothing more. The real question is where is obj.h located on disk. It must be in the same folder where .cpp is, which you want to compile. If it is not on the same folder then you:
move obj.h in the same folder where .cpp is, or
change #include directive to be a relative path beginning from .cpp and ending to obj.h, or
set option in project that tells compiler where to find obj.h (Properties > C/C++ > General > Additional Include Directories), or
set option in Visual Studio that tells compiler where to find obj.h. This option is used for MSDN, or some other framework that you rely on heavily, and it should not be used for your own files.