What is the best method to include a file that has same name in another folder from additional include directories?
Example:
lib1/include/foo.h
lib2/include/foo.h
where both lib1/include and lib2/include are added in additional include directories.
Edit:
The libs are from different SDK's and every developer installs them in his own place. Only thing that is sure is that both folders are in IDE's additional include paths
method 1:
#include "../../lib1/include/foo.h
method2:
Add lib1/include before lib2/include in search paths and because they are searched in order with:
#include "foo.h"
lib1/include/foo.h will be included
To avoid multiple inclusions of the same header file we use the #ifndef, #define and #endif preprocessor directives. Just write the entire program in only file and include headers once only.
If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.
No. Header File is the file where all the headers name are mentioned that going to be used or consumed in the main code file. On other hand Library is the file where the implementation code of each header is written down which is mentioned in the Header file.
If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.
#include "lib1/include/foo.h"
#include "lib2/include/foo.h"
Is fine as long as this is the actual relative path to those headers and the include guards are different. For example, if both foo.h use
#ifndef _foo_h_
then this will give you something you dont want (it will only include one, not both and which one depends on the order of execution).
First off, this answer assumes that the include guards for the two headers are compatible (i.e. not the same symbols).
One thing you can do is create links in known locations to the header files of interest, giving the links themselves distinct names. For example, say your two libraries are installed at $LIB1PATH and $LIB2PATH, which could have different values in different build environments. Thus the headers you want to get are at $LIB1PATH/include/foo.h and $LIB2PATH/include/foo.h.
You could go two ways with this. One is by creating direct links. This could look like this in your project's directory tree:
$PROJDIR/
include/
lib_include/
lib1_foo.h -> $LIB1PATH/include/foo.h
lib2_foo.h -> $LIB2PATH/include/foo.h
src/
This could get tricky if your code is in a repository, because you couldn't check these links in; they'd be wrong in other environments. Also, if you have a lot of these links and few libraries, you'd have to recreate all of them whenever lib1 or lib2 move... not cool. You can get around this problem by creating links in the directory that contains the project's directory:
$PROJDIR/
include/
lib_include/
lib1_foo.h -> ../../lib1/include/foo.h
lib2_foo.h -> ../../lib2/include/foo.h
src/
lib1 -> $LIB1PATH/
lib2 -> $LIB2PATH/
In both cases, you need to make sure $PROJDIR/lib_include
is on your include path. Also, you only need to have $LIB1PATH/include
and $LIB2PATH/include
in your include path if the two foo.h
headers pull in more headers from those directories. You could also put the links in include
and get rid of lib_include
, but I like keeping these things separate.
You can just do this:
#include "lib1/include/foo.h"
#include "lib2/include/foo.h"
and make sure that the parent directories of both lib1
and lib2
are in your search path for includes (but not the actual include
subdirectories themselves).
Note that if both headers use the same symbol as an include guard then this method will not work - you would need to undefine the conflicting symbol between the two includes:
#include "lib1/include/foo.h"
#undef FOO_H
#include "lib2/include/foo.h"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With