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
-
1-I parameter to compiler lets you specify include paths.– ZimbabaoFeb 27, 2011 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 SagarJan 7, 2016 at 18:05
-
Related: c - Including a header file from another directory - Stack Overflow (includes some more discussion about <> / """– user202729Jan 29, 2021 at 8:27
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)
-
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? Aug 24, 2017 at 14:32 -
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