1

I am working on a code for ESP boards that can be used on either ESP32 or ESP8266 and I want to make sure I include the correct libraries.

I know I can check if ESP32 OR ESP8266 are defined, but I wanted something more specific.

Anyway, in my project I have a class called Storage which provides an interface for a storage medium, and two implementations: one with the Preferences library and another with SPIFFS.

I want to have instantiation functions for each implementation (sort-of factory pattern), and to use pre-compilation checks to only compile the valid implementations for the currently selected board.

This is my current code:

#include <memory.h>
#include "Storage.h"

#if __has_include(<Preferences.h>) #include "PreferencesStorage.h" std::unique_ptr<Storage> createPreferencesStorage() { return std::make_unique<PreferencesStorage>(); } #endif

#if #if __has_include(<SPIFFS.h>) #include "SpiffsStorage.h" std::unique_ptr<Storage> createSpiffsStorage() { return std::make_unique<SpiffsStorage>(); } #endif

My problem is that #if __has_include(<Preferences.h>)returns false for some reason even though I can import the library. It only returns true if I first include <Preferences.h> which obviously is not really helpful.

Does anyone have idea what might be the cause?

SagiZiv
  • 201
  • 1
  • 8

0 Answers0