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
Related
I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:
C:\\Projects\\MyProgram
The header file is located in the folder:
C:\\Projects\\MyProgram\\Files
I tried the following line of code, but it doesn't work.
#include <Files\MyHeader.h>
Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?
Thanks in advance for any help. :)
Try this
#include "files/myheader.h"
It will work if the header is in a files folder in the same directory as the current source.
If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).
In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.
Double Click on the Project Name. You will see a dialog box like this:
Make sure All Configurations is chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.
Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.
You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately
Add the library path the same way to Library Directories
Save the changes using the Save button on the Property Manager Pane's toolbox.
You will then be able to access the header file contained in the directory you added by something like:
#include <myheader.h>
This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.
The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.
The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.
If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).
Ideally you should use forward slashes in paths too.
Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir) I think.
That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.
To use a relative path in your cpp file without any include directory settings, you might need something like:
#include "../Files/MyHeader.h"
You just need to replace your brackets <> with double quotes "" like this:
#include "Files\MyHeader.h"
Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.
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'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.
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.
I'm using VS2010 (downloaded via dreamspark) and although I can open the #include file by right clicking on it and pressing on Open Document, it complains "Error can not open source file "..."" which seems rather absurd. I'm using Qwt with Qt this time around and I'm specifically having the problem for:
#include <qwt_counter.h>
#include <qwt_plot.h>
(And I am using the "<>"); not sure how to make those appear properly in the code above.
Thanks in advance.
As Neil indicated, try using quotes instead of the <> characters around the filename. When using the quotes, MSVC will look in the same directory as the file the #include is in for the specified file, then if it's not found there will look in the directories specified by the include path. When the filename is surrounded by <> characters, the current file's directory isn't looked at - the compiler goes right to the include path.
See http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx for details.
Note that this is an implementation dependent behavior - it might not apply to other compilers.
If that doesn't help, make sure that your include path contains the directory that the file is located in by setting the "Include Directories" property appropriately:
http://msdn.microsoft.com/en-us/library/t9az1d21.aspx
Finally, you might be using a makefile project (I'm not sure how common it is for Qt projects to continue to use qmake when built from VS) , in which case you'll need to perform whatever configuration is necessary in the make file(s) or parameters passed on the command line that invokes the makefiles.
Is the path where these files are located either the same as that of this source file, or included in the "additional include directories" in your project settings?
Project -> properties -> c/c++ section -> additional include directories.
If they are located in a subdirectory of the source file you're editing or of one of the additional include directories (I think) you can also include them with:
#include <path_to_file_1/qwt_counter.h>
#include <path_to_file_2/qwt_plot.h>
[edit]
or of course what neil says
[/edit]
It turned out there was a circular linking happening and I had all my code in a .h file. I split it up and added the corresponding .cpp file, now everything works fine.