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
Asked
Active
Viewed 5.0k times
19
-
1-I parameter to compiler lets you specify include paths. – Zimbabao Feb 27 '11 at 16:36
-
HI , i have a dir include and src which contains the header files and .c files respectively. I am getting error when i try to compile with -I option. gcc -c -I ./../include tree.c , as tree.c also include stack.h, it is not picking up stack.h – Abhishek Sagar Jan 07 '16 at 18:05
-
Related: [c - Including a header file from another directory - Stack Overflow](https://stackoverflow.com/questions/7581408/including-a-header-file-from-another-directory) (includes some more discussion about <> / """ – user202729 Jan 29 '21 at 08:27
2 Answers
35
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)

Mike Caron
- 14,351
- 4
- 49
- 77
-
I'm getting `unresolved external symbol` for any class/method. I also want to do it like this with **cpp** & **h** files in a solution directory, but utilizing "Include Directories" and "Source Directories" seems to be not enough. Any idea? – bytecode77 Aug 24 '17 at 14:32
-
3
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

Mircea Ispas
- 20,260
- 32
- 123
- 211