-2

I have a problem with including .h file

Here a screenshoot:

enter image description here

How do I include B.h file in C.c file?

t.niese
  • 36,631
  • 8
  • 65
  • 95
Peter
  • 3
  • 2
  • What have you tried? There's no simple answer to this because it depends on your compiler and the compiler options you are using. – john Jan 29 '21 at 08:22
  • 3
    Possible duplicate of [how to include header files in other src folder](https://stackoverflow.com/questions/5134357/how-to-include-header-files-in-other-src-folder) – user202729 Jan 29 '21 at 08:22
  • I'm using currently Visual Studio – Peter Jan 29 '21 at 08:23
  • @Peter And where is your project located in relation to the header fies? – john Jan 29 '21 at 08:23
  • You have a directory `libs` that indicates that you have 3rd party libraries you want to use in there. So based on the library structure you add the correct directory of that library to the list of include directories of your project/compiler and then include the file with a path relative to that include directory of that library. – t.niese Jan 29 '21 at 08:24
  • libs folder is in the project folder – Peter Jan 29 '21 at 08:25
  • @Peter Then I would try `#include "glad/a/b/B.h"` – john Jan 29 '21 at 08:26
  • I tried, but i didn't work. But when I used #include "../a/b/B.h", it worked. – Peter Jan 29 '21 at 08:28
  • OK, well it depends upon the 'Additional Include Directories' setting under your project properties. Include paths that are relative to the including file seem a little fishy to me since if you move the file you need to change the include. – john Jan 29 '21 at 08:30

2 Answers2

0

Assuming libs is a directory for distinct libraries, specify it as an include directory and include the header like this:

#include "glad/a/b/B.h"

when I used #include "../a/b/B.h", it worked.

I would recommend against including files relative to the current file. Instead, always include relative to an include directory. Following this convention makes it easier to search all instances where a header is included within a project.

eerorika
  • 223,800
  • 12
  • 181
  • 301
0

1: Include it relatively like:

#include "../a/b/B.h"

2: Adding additional include directories in the visual studio. (Project properties > C/C++ > General)

Majid
  • 11
  • 3