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.
Related
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.
I'm trying to write a program using flex++, however everytime I try to compile I receive the error message:
FlexLexer.h: No such file or directory
However that header is found on the include folder of flex. I don't have problems compiling lex programs for c, however for c++ with flex++ I can't seem to find a way.
I already downloaded flex various times and I don't know if there is a problem with my OS or something like that. My OS is Windows 10.
Thank you
Including should be pretty straightforward once you understand how it works.
Let's look at some different ways you can include a file:
#include "FlexLexer.h"
The quotes tell the compiler to look for the file FlexLexer.h in the same folder as the file being compiled. That is it, it won't look anywhere else.
Now if we change the quotes to brackets:
#include <FlexLexer.h>
This tells the compiler to look for FlexLexer.h in the same folder first, but then if it isn't found it will go through the list of include paths looking for it there, too.
Assuming you are using VisualStudio, there is both a system list of include paths (see Tools > Options > Projects and Solutions > VC++ Directories) and a project list of include paths (right click on the Project in the Solution Explorer, Properties > VC++ Directories). Both of these lists are traversed.
Finally, you can also add subdirectory qualifies to the include, e.g.:
#include "Win\FlexLexer.h"
or
#include <Win\FlexLexer.h>
As you might guess, it looks for the path under either the current directory in the both examples and also under the include path list in the later example. Regardless, the first time the file is found the search will stop and the compiler will use it. So be careful if there are headers will duplicate names in different libraries!
So a few hours ago I started learning c++ in codelite but I was getting frustated with, so I just got codeblocks and imported the project. But now whenever I try to compile it returns:
fatal error: imports.h: No such file or directory
This is my project hierarchy in codeblocks:
And this is what the project folder looks like:
What am I doing wrong?
I know this is years later, but I've recently seen students following what I deem is frankly bad advice such as what is given above. For those learning c++ this functionality is NOT for you. To add headers you should simply check that you are using double quotes, instead of angled brackets i.e.
#include "myheader.h"
and NOT
#include <myheader.h>
Angled brackets are meant for libraries (informally) and adding a simple header file for you basic classes does not require you to change default search directories. The problem comes when someone else tries to run your code (assuming you're doing this for uni) and their IDE isn't setup to search for a "library" (your header) where it shouldn't be. Double quotes tells the compiler the files exist in your current relative directory. This way you can keep your main, headers and header implementation in one directory. Fiddling with your IDE should only be done when necessary. KISS
You have to tell Codeblocks where to find the header files that you include. Try adding the full path to your '/Headers' in the include directories of codeblocks
Goto 'Codeblocks menu > Settings > Compiler > Search directories > Add'.
EDIT: Since your issue, however, is quite irrelevant to learning the C++ language itself, I suggest that you start with simpler programs, then move on to more complex ones. That, of course, unless you have previous experience with other programming languages
Since I haven't found any Makro for
#define 'hostname of device where compiler is located' // which is unique and not to be copied !
I have now successfully used and included
#include "myCompileEnv.h"
as a workaround with the comments above, which is located more central - above the project directories in CodeBlocks.
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.
If I simply include a file by writing
#include "blah.h"
where exactly does the compiler search for this file? I understand that there are limitations.
What happens if the file is not in the same folder, but much deeper in the structure, how do I tell the compiler to look there? equally if it is above the file in the directory? or maybe deeper in a different branch?
Essentially I don't have a grasp of how you navigate around the structure. I've seen some includes that look something like:
#include ".././foo/whatever/blah.h"
what do the dots mean? they go back up but do different numbers mean different things?
Also is this based on the structure of the files on the computer or their structure in the solution explorer?
Thanks very much for the help on this one, I understand this is a bit of a basic question - just one of those things I never learned.
From the visual studio documentation on #include
The preprocessor searches for include files in the following order:
In the same directory as the file that contains the #include statement.
In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
Along the path specified by each /I compiler option.
Along the paths specified by the INCLUDE environment variable.
You can change the paths passed to the compiler via /I options in the visual studio project settings (for project specific paths) and in the visual studio options for global paths.
2 dots in a path move you up a directory, one dot refers to the current directory. Any other number of dots would not be valid. So your example path of .././foo/whatever/blah.h essentially means "move up one level, look in a folder foo, then look in a folder whatever". The single dot in this case doesn't really do anything.
This navigation is based on the file structure, not the structure in solution explorer
When you write #include "a.h" in a.cpp, then preprocessor searches a.h in the same directory where a.cpp is. If this search is not supported, or if the search fails, then preprocessor searches a sequence of implementation-defined places for this a.h.
"what do the dots mean?"
Lets say you have file with this fullpath: C:\myDir\myProjects\a\a.cpp:
. = C:\myDir\myProjects\a
.. = C:\myDir\myProjects
You should also take a look at What is the difference between #include <filename> and #include "filename"?