2

I am new in programming I am using vscode in windows and compiling via Mingw 64. I am requesting an answer to this question after tried of trying for days. I googled it many times. here is how my project tree looks. please help me include and link libA to libB and common.h to all cpp files and libA and libB to to main.cpp. Please answer.

project
|---lib
|   |---libA
|   |   |---header1.h
|   |   |---header1.cpp
|   |
|   |---libB
|       |---header2.h
|       |---header2.cpp
|---common
|   |---common.h
|   |---common.cpp
|---main.cpp
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Does this answer your question? [including header files from different directories?](https://stackoverflow.com/questions/8621507/including-header-files-from-different-directories) – David C. Rankin Aug 27 '20 at 04:36
  • actually I am unable to solve error: ` **No such file or directory** : gcc` – Jaikaran saini Aug 27 '20 at 04:44
  • include path is ok but gcc can not find header. – Jaikaran saini Aug 27 '20 at 04:46
  • In that case I would right-click the project (over in the project panel at the right) and choose -- properties. In C/C++ option you can add additional compiler options and just include `-Icommon` and `-Ilib` (you may have to include the full path as `-I./lib`, etc.. I don't recall how VS takes the path. If you have the includes found then, and you have the `.cpp` files as part of the project, it should compile and link them when you build the project. – David C. Rankin Aug 27 '20 at 04:53

2 Answers2

3

From VSCode, you can right click on the headers and select Copy Path. That should work when including headers from anywhere on your computer.
As for the .cpp files, you could use relative paths to the directory you're compiling in, when compiling and linking the files, or use the -I option when compiling with g++.
This might help.

  • 2
    Good job adding the link. But note for the future -- when a question already has an answer on this site, you are supposed to click "close" and then select "Duplicate" and enter the link. That prevents fragmenting different answers for the same question on the site. – David C. Rankin Aug 27 '20 at 04:38
1

You should use the relative path in #include.

For example to import header1 use -#include "./lib/header1.h" and to import common use #include "./common/common.h"