Pass Libraries to included files

Hi, I’m really a newbie to cpp. (Migrating my project from Arduino IDE to awesome PlatformIO). I got simple questing but I can’t make it work even after a few hours.

I got main.cpp - basic main file where I define all my required includes setup etc. And I want create file callbacks.cpp where I will place my callback function. But I get :
src\callbacks.cpp: In function ‘void connectWiFi()’:
src\callbacks.cpp:15:5: error: ‘WiFi’ was not declared in this scope

of coarse because in callback.cpp in not Wifi library included on top of file. But library include in main file is dependent on board type etc( so this incldes are really long and complicated ) and I really do not want copy and paste this bunch of code to every separate file.

Example of main.cpp

//Basic ESP8266 includes
#if defined ESP8266 //include header files by board type
  #include <ESP8266WiFi.h>
  #ifdef USE_MDNS
#include <DNSServer.h>
#include <ESP8266mDNS.h>
  #endif
  #ifdef WEB_SERVER_ENABLED
#include <ESP8266WebServer.h>
  #endif
#elif defined ESP32
  #include <WiFi.h>
  #ifdef USE_MDNS
#include <DNSServer.h>
#include "ESPmDNS.h"
  #endif
  #ifdef WEB_SERVER_ENABLED
#include <WebServer.h>
  #endif
#else
  #error "Now RemoteDebug support only boards Espressif, as ESP8266 and ESP32"
#endif // ESP

example start part of callbacks.cpp
/**
* Callback functions
*/

// WiFi callbacks
void connectWiFi()
{
#ifdef ESP32
    // ESP32 // TODO: is really necessary ?
    WiFi.enableSTA(true);
    delay(100);
#endif
// Connect with SSID and password stored
#ifndef WIFI_SSID
    WiFi.begin();
#else
    WiFi.begin(WIFI_SSID, WIFI_PASS);
#endif

So how is possible to “pass all libraries from main to all other included files”.
Thanks guys.

Why not offload the #if def.. code in a main.h file which will be included by both main.cpp and callback.cpp?

2 Likes