Add Arduino Library to ESP-IDF with Arduino core as component?

I’m trying to add an Arduino Library ( u8g2 ) to an ESP-IDF with Arduino Core as component Project, but I’m having issues to do so.
First thing I did is I cloned the espidf-arduino-blink example (Link) and opened it as a new project in VSC PlatformIO.
I then went to the Library manager of PlatformIO, searched for the library and added it to the Project.
After that, I started the Setup process according to the Library wiki.
However, when I add the Constructor (for example,

U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 8);

VSCode marks it with issues and tells me the identifier is undefined. However, If I go to the reference link, VSCode goes to the correct Class.

I’m a newbie and I don’t know much about C/C++ and, yes, this is going over my head. I tried using Arduino Core instead of Arduino as component and it works, so I could continue using that, but I want to learn and understand what’s happening and why it’s happening.
So, basically, the Constructor shows error “identifier undefined”
My code basically looks like this:

/* Blink Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "sdkconfig.h"
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>


void setup() {
void loop() {
  do {
    u8g2.drawStr(0,24,"Hello World!");
  } while ( u8g2.nextPage() );

Does the project build in PlatformIO? Does the error show up after that, too? (Intelliesense errors are not always build errors and a result of the library bein temporarily not yet there yet or IntelliSense paths not being updated yet). If yes, does a project tasks → Miscellaneous → Rebuild IntelliSense help?

It doesn’t
Here’s the error:
Compiling .pio/build/esp320/esp-idf/src/Blink.cpp.o src/Blink.cpp:29:1: error: 'U8G2_SSD1306_128X64_NONAME_F_HW_I2C' does not name a type U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 22, /* data=*/ 21);

Aha, I can reproduce the error.

You see, all these classes (types) are only defined if

and that is only defined if

But when building in ESP-IDF + Arduino mode, apparently the ARDUINO macro isn’t set and thus also not the U8X8_USE_PINS macro. (@valeros should that be fixed in the builder code maybe?)

Modify your project’s platformio.ini to use another build_flags directive like

platform = espressif32
framework = arduino, espidf
build_flags =
    -D U8X8_USE_PINS
monitor_speed = 115200
platform_packages =
  ; use a special branch
  framework-arduinoespressif32 @
lib_deps =
     olikraus/U8g2 @ ^2.28.8
board = esp32dev

and it builds for me.

Linking .pio\build\esp32dev\firmware.elf
Building .pio\build\esp32dev\firmware.bin
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.6% (used 15052 bytes from 327680 bytes)
Flash: [==        ]  18.6% (used 194709 bytes from 1048576 bytes) v3.0
============================================= [SUCCESS] Took 57.97 seconds =============================================

Thank you for the research and answer @maxgerhardt
Would it be too much trouble if I asked you more about this?

I’ve done the same backwards path in an Arduino framework project and also found that there’s no
#define ARDUINO
macro to be found, at least not in the project files. But the project builds without any build_flag
Why is this?

The Arduino-ESP32 builder script that is called by PlatformIO defines these macros.

Apparantely in ESP-IDF + Arduino mode, just the ESP-IDF builder script takes over but doesn’t inject these macros.

The Arduino-IDE does that via a platform.txt instruction.


Ok, I’m getting in a rabbit hole. That might take me a while to understand, but thank you for the patience and for replying.

Getting back to the business, that’s why you asked @valeros if this needs to be added to the builder or not, correct?
It might be an overlook or could be intentional, correct?

The Arduino IDE encodes the version of itself, e.g. “1.8.5” in the ARDUINO macro as this integer, and since it’s passed to the compiler as a -DARDUINO=10805 flag macro value is globally visible in any file, so some libraries use this macro to determine in what environment they’re running in, nothing wild :smiley:

Yes exactly, he’s a main developer of the platforms. It might be correct that a ARDUINO macro is not added, or it might not be. But for you this seems to be right thing to make the library happy.