Adding pybind11 library to platformio to wrap c++ code

I have a library which was originally written for Arduino UNO board. I am trying to wrap up a part of C++ code from this library using pybind11 module. The requirement of this pybind11 is to have C++11 compatible compiler (as mentioned in “pybind11 documentation”). I tried compiling this library using Platformio in VSCode and it was successful. Now when I add pybind11 library, it throws include errors even after the path being added to both c_cpp_properties.json as well as tasks.json.

Has anyone tried anything similar previously and was successful?
Also can someone please explain what and how the compiler is invoked in Platformio in VSCode?

Thanks in advance!

You don’t add include paths by editing the VSCode JSON files. These are just for VSCode and autogenerated by PIO from the project configuration and they’re not used for compilation. Include paths are derived from the recognized libraries (Library Dependency Finder (LDF) — PlatformIO latest documentation) optionally build_flags.

If this pybind11 module is some C++ code, you probably want to create a new library folder in lib/ for it and write a library.json for it.

Thanks for your valuable inputs!
Yes I do understand that JSON files are autogenerated. But like I mentioned since I was running into include errors the red squizzly lines on the headers suggested the path to be added to include path and I so did that.

Coming to the second part, pybind11 is a header only library that has dependency on python and C++ libraries. And I tried adding a new folder in lib/ and dumped the pybind11 into it and created a library.json. With this the dependent files are not getting included (Like: <Python.h>).

Sorry, I am trying my hands on Platformio for the first time and hence the lack of knowledge. Could you please guide me here with further steps?

If the library needs a Python core compiled in, it’s impossible on an Arduino Uno. In 32Kbytes of flash and 2KB of SRAM, no Python interpreter or core can fit.

You may have more luck looking at Micropython with the ESP8266 or ESP32.

Hey Max! Yes I do understand the fact that a python core cannot fit into an Arduino UNO . I think I have not put it correctly. Let me re-iterate my use case.

  1. I have a sensor library written in C++ which was developed for Arduino UNO board initially.
  2. Now I am trying to make the library platform agnostic so that I can use this library on other hardware platforms like RaspberryPi which means a single core library running on different platforms.
  3. In order to do so, I want to extend existing C++ code (part of library that will not use any arduino specific functions) by wrapping them using pybind11 and import this module to python and for the arduino specific functions python modules will be called by embedding the python interpreter.
  4. Now to create these bindings I need to run the code at least once as is and hence the need of platformio (though it wont be actually flashed into aduino uno board).

So you see its just the case of getting the code compiled so that the wrappers are generated and can be used in python. It wont be flashed into Arduino board.

Hope this explanation clears the use case of why I need pybind11 in platformio.

Thank you for your time and inputs! I would like to know if you have any thoughts on this further. Meanwhile, I would also check if micropython can be applicable in this case!

But if you only want to create Python bindings for the platform-indendent code, why not compile that code normally on the desktop for x86/64?

PlatformIO can also compile native programs through the native platform if you still want to use PlatformIO for it.

If the result just has to “compile” but not actually be uploaded, you might be able to hack the compilation process by

  1. really including the Python core and all its dependencies in the project
  2. hacking the maximum RAM and FLASH sizes (platform-atmelavr/boards/uno.json at develop · platformio/platform-atmelavr · GitHub) with platformio.ini like board_upload.maximum_ram_size = 99999999, board_upload.maximum_size = 999999999.
  3. hope that the comppiler is able to emit code for what will be a very large binary… it may be that due to the 8-bit AVR nature, it has a 16-bit adddress space resulting in only being able to compile a maximum of 64kByte of code.