Header files in separate directory; how to set up include folder?

I have main.c and main.h files in the src directory (VSCode project for ESP32 Dev). Everything works fine, project builds, it just works.

But I’d like the header files to be in a different directory than the source files, like include. With this, I have so far been unsuccessful. Moving the header to include was recognised by VSCode, but the building failed, telling me that pretty much everything is undefined. lib acted just the same. I tried putting build_flags = -I include to platformio.ini or include_dir = include, but the effect was always the same (and yes, every attempt, I did remove the .pio directory).

I tried to look for solution here but I either just got more confused or the issue was relevant to IDE errors – VSCode recognises the header files and Intellisense works just fine, the build system doesn’t.

What am I doing wrong/what am I missing?


Duplicate of

That explains why the error happens:

Correct, .c/.cpp files in include/ are not compiled ("added to the build system to produce a .o object file that will be linked in the final .elf).

…but doesn’t say what to do about it. Do I just have to go with it and make a src/include directory, if I want the headers, ex. main.h, included with main.c? What’s the point of include directory then?

EDIT: Okay, just randomly it started behaving differently. With the build_flag = -I include thing, it seems to recognise the headers (I think?) but I am getting new errors about the ESP headers not being recognised:

In file included from /home/onegen/.platformio/packages/framework-espidf/components/wpa_supplicant/port/eloop.c:16:
include/common.h:12:10: fatal error: driver/gpio.h: No such file or directory
   12 | #include "driver/gpio.h"
      |          ^~~~~~~~~~~~~~~
compilation terminated.

Same problem occurs if I try the src/include thing.

Did you find a solution eventually?

Please describe the exact problem you have incl. your file structure and the error message.

Basically it is pretty easy:
put .h files in ./include
put .cpp files in ./src

This applies to files you have written by yourself, not for libraries.