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?

TY.

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.

Hello Everyone, New here.
I am trying out PIO with VS Code on windows, and all I want to do right now is include the gpio.h file without having to first copy it into the include folder in my project dir.
here is the error I keep getting:
src\main.c:1:10: fatal error: driver/gpio.h: No such file or directory
1 | #include “driver/gpio.h”
| ^~~~~~~~~~~~~~~
compilation terminated.

Any help with setting up PIO would be greatly appreciated.
also, $pio run returns a not recognized as an internal or external command message.

Based on the tags you have chosen, i suppose you’re making an ESP-IDF project.

Usually there is no need to copy header files which are part of the esp-idf framework into the include folder because they are already “builtin”.

A simple #include "driver/gpio.h" should work (and does for me).

My experience after setting up a fresh ESP-IDF project:
After adding the #include "driver/gpio" to main.cpp, it took a momment and a required a restart of VS Code to work correctly.
But maybe I didn’t wait long enough and the restart is probably unnecessary.

After that the file was processed by IntelliSync without any problems.

In any case, a compilation error should never occur. Then there really seems to be something wrong with the installation. Perhaps the framework was not yet fully installed?

I have worked with PIC processors for year, but this is my first time jumping into ESP. I expect the driver/gpio.h include to work, according the resources online, and the ESP development manual i have, however, it is not. do I need the ESP32 device connected to compile? and can you point me to an install guide? so I could try reinstalling.

No, for compiling this is not necessary.

Do you have an ESP-IDF project or Arduino?
I guessed ESP-IDF regarding the tags, but I’m not sure.

ESP-IDF project.
I have also reinstalled PIO following the official documentation and nothing changed. I also tried an arduino project, and IDE returned the same error for #include “Arduino.h”: No such file or directory gcc.
image

Reinstalled the VS Code extension?
Or did you clean the platform and package files?

Which one? Please share the link, so I can see what you have done so far.

You mentioned intelli
The VS code extension is what I reinstalled.

Any idea what this means:
image

and if it could be contributing to my error?

Unfortunately, this won’t fix any platform related issues.

  • close VS Code
  • delete C:\Users\<username>\.platformio\platforms\espressif32
  • restart VS Code
  • open your project
  • wait until platformio has completed all the necessary tasks
1 Like

I think I identified my problem;
image
I keep trying to compile using the default C/C++ compiler on VSCode, which keeps throwing up that error.


Here is what the output looks like, how do I change the the default compiler, and what should it be?

No, you have to use PlatformIO’s toolbar at the bottom!

See
https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-toolbar

Here you’ll find a video tutorial

1 Like

You have been most helpful.
It works, I can’t believe something as fundamental as this has held me up for over a week. Thanks for your patience.

1 Like