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
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.
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 .
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.
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.
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)
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
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