PlatformIO Community

Unable to specify an include directory

Hi, This topic seems to have been beaten to death now but in spite of me reading so many solutions on the internet, i am unable to do this simple thing.
I am switching from Arduino IDE to Platformio and having trouble specifying an include path for my environment. Can somebody please help me out here. details below

I have my project under the path :

C:\My Data\OneDrive\Documents\Circuits\home_automation\Water Flow Senso

I have some common include files i keep for all projects of mine and they are located in

C:\My Data\OneDrive\Documents\Circuits\home_automation\include

I am using the following in my main.cpp but it given an error that secrets.h not found:

//common files from /include
#include “secrets.h”
#include “version.h”
#include “myutils.h” //common utilities

I have tried the following solutions:
Solution 1:
Changed the include path in c_cpp_properties.json to

        "includePath": [
            "C:/My Data/OneDrive/Documents/Circuits/home_automation/include",
            "C:/My Data/OneDrive/Documents/Circuits/home_automation/Water Flow Sensor/include",
            "C:/My Data/OneDrive/Documents/Circuits/home_automation/Water Flow Sensor/src",

Solution 2:
Included the directory in platform.ini file under the env i am building (I only have one env)
include_dir = “C:/My Data/OneDrive/Documents/Circuits/home_automation/include”

Pl let me know what i am doing wrong. The solution which works is if i copy all my .h files into the directory C:/My Data/OneDrive/Documents/Circuits/home_automation/Water Flow Sensor/include but i dont want to do that because these files are used by other projects too.

No. This “fixes” the IntelliSense by adding it to the header-lookup paths of VSCode, but PlatformIO does not use this file – it fills it with the build information as configured by the PlatformIO. This will not build.

The problem with this is that now the include folder of the project would be ignored. This is not necessary.

As the header of the file tells you (and also the platformio.ini links you to that), you need to add a build_flags expression with the appropriate flag to add a include directory. This is an additive option (as opposed to changing include_dir).

build_flags =
  -I"C:/My Data/OneDrive/Documents/Circuits/home_automation/include"

note that this only works if you really only need to add .h files. If there is a .c/.cpp implementation behind it, and it’s not header-only, this will result in a linking error, since the .c/.cpp files are not added to the build system. In that case, you need to restructure your helper files to a library and e.g. use lib_deps with a file://<path> expression, or lib_extra_dirs.

Great, thanks for the answer and clear explanation. I had tried what you suggested earlier but maybe i was not specifying the path properly and it didn’t work. I did it now and it works. Although I ran into a different issue of including other files from that path. If you don’t mind , can you also suggest what’s wrong in here.

One of the files within C:/My Data/OneDrive/Documents/Circuits/home_automation/include is the file ESPOTA.h which has a #include <Arduino.h> within it.
When I include the file ESPOTA.h it gives me an error that <Arduino.h> not found while if I include Arduino.h directly in my main.cpp , it works fine.
Is there a difference between including files from a separate path and libraries?

My main.cpp:

#define USE_OTA
#define VERSION “1.2.1”

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP8266WebServer.h>

//common files from /include
#include “secrets.h”
#include “version.h”
#include “myutils.h” //common utilities
#include “Debugutils.h”
#include “ESPOTA.h” //OTA capability
#include “websocket_log.h”


#ifndef ESPOTA_H
#define ESPOTA_H
#include <ArduinoOTA.h>
#include “Debugutils.h”

#ifdef USE_OTA
#define SETUP_OTA() setupOTA()
#define HANDLE_OTA() ArduinoOTA.handle();

The error I get

In file included from src\main.cpp:7:
C:\My Data\OneDrive\Documents\Circuits\home_automation\include/ESPOTA.h: fatal error: ArduinoOTA.h: No such file or directory

This is probably an edge case where the library dependency finder can’t identify the dependency on ArduinoOTA. Try to #include <ArduinoOTA.h> in the main.cpp file, or add it to lib_deps in the platformio.ini. Maybe even a different lib_ldf_mode helps.

Hmm, that of course does work but still wondering why does it not recognize from within ESPOTA.h. Thank you very much for your help !