Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include header files in other src folder

I have a c++ project having two src folders. Source file in folder 1 may need to include header file in src folder 2. Is it possible? or how should I write my Makefiles? thanks

like image 635
Richard Avatar asked Feb 27 '11 16:02

Richard


People also ask

How do you put a header in another folder?

You can find this option under Project Properties->Configuration Properties->C/C++->General->Additional Include Directories. and having it find it even in lib\headers. You can give the absolute or relative path to the header file in the #include statement.

How do I include a .h file in another folder in CPP?

You can use the /I command line option to add the path or set the path in your project settings. Show activity on this post. 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 .

Can header file include other header files?

Including Multiple Header Files:You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.

Which one is another way to include header files?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.


2 Answers

Depending on how closely the two folders are related (eg, if they're the same project), then it can be as easy as:

#include "../otherfolder/header.h"

If they're separate projects, then it's customary to simply add the other project's header directory to your project's header search path, and include the header like this:

#include <header.h>

(In practice, the brackets/quotes don't matter, but it helps keep external vs. internal header imports separate)

like image 196
Mike Caron Avatar answered Sep 21 '22 15:09

Mike Caron


Considering you have src1 and src2 folders in same folder. You have 2 solutions for this:

1 - #include "../src2/header.h"

2 - Add in your project at additional include directories src2 and use normal #include

like image 34
Mircea Ispas Avatar answered Sep 23 '22 15:09

Mircea Ispas