Include search path redundency platformio.ini and vscode settings include path

Hi Friendly Forum Members:

Each time I start a new project, of course as expected, I must add a search path in the build_flags section in platformio.ini to include my custom header files.

However, to prevent an annoying underline at #include <Myheader.h>, I muse ALSO add the path in VSCode settings include path.

I understand the paths in VSCode are in c_cpp_properties.json in my project folder.

Some questions in order of preference are…

First, (most preferred) Is there a way to make VSCode glean the paths from the build_flags section in the platformio.ini file?

Second, How is the c_cpp_properties.json file created, and can I modify the source for creating that file?

Third, Can I change the default VSCode include path to include an environment variable that has my include paths such that new projects automatically have them?

Fourth, (least preferred) setup VSCode default to include my custom paths in each new project.

Thanks for your help,

Mark.

You don’t have to use the build_flags for this.

Here is a workflow / file structure that solves your problems.

  • Convert the header files into libraries (by adding some sub folder’s and the library.json)
  • Create a folder structure for your projects and local libraries like so:
.
├── libraries
│   ├── LibA
│   │   ├── src
│   │   │   ├── LibA.cpp
│   │   │   └── LibA.h
│   │   └── library.json
│   ├── LibB
│   │   ├── src
│   │   │   └── LibB.h
│   │   └── library.json
│   └── LibC
|       └── ...
└── projects
    ├── projectA
    │   ├── .pio
    │   ├── .vscode
    │   ├── src
    │   │   └── main.cpp
    │   └── platformio.ini
    ├── projectB
    |   └── ...
    └── projectC
        └── ...

Use symlink in the lib_deps section in the platformio.ini for the corresponding projects:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps = 
  symlink://../../libraries/LibA
  symlink://../../libraries/LibB

You can also use absolute paths names for your libraries.

Then simply include the libraries to your project:

#include "LibA.h"
#include "LibB.h"

Another way is to host your libraries on github:

lib_deps = 
  https://github.com/sivar2311/LibA
  https://github.com/sivar2311/LibB

This even allows you to use different versions of your libraries in different projects.

projectA:

lib_deps = 
  https://github.com/sivar2311/LibA @ 1.0.0

projectB

lib_deps = 
  https://github.com/sivar2311/LibA @ 1.3.2
  https://github.com/sivar2311/LibB @ 2.0.0

This is more the style of the ArduinoIDE where all global installed libraries are included automatically in every project.

In PlatformIO you include libraries per project and only those who are needed by the project.


Documentation:
Creating Library
Using local libraries with symlink