Cross-post from stackoverflow:
I’m trying to get started with PlatformIO and have hit a wall with include priorities and/or variable scopes.
My code consists of a src/main.cpp
program as well as several private libraries which reside in the lib/<libname>/*.cpp/h
location.
I want to have a global object of an external class (Syslog) which is accessible to both my main program as well as any private libraries I’ve added.
I’ve currently tried 2 approaches, which both refuse to compile:
Create the object in main.cpp with the extern keyword, initialize the object in setup()
like so:
src/main.cpp:
#include <Syslog.h>
#include <WiFiUdp.h>
WiFiUDP SyslogUDP;
extern Syslog syslog;
void setup()
{
syslog = Syslog(SyslogUDP, "255.255.255.255", 514, "FlyballETS", "FlyballETSApp", LOG_INFO, SYSLOG_PROTO_BSD);
}
lib/GPSHandler/GPSHandler.cpp:
void GPSHandlerClass::init(HardwareSerial * SerialPort)
{
syslog.logf_P("GPS Class initialized!");
}
This results in compiler errors like:
lib\GPSHandler\GPSHandler.cpp:22:4: error: 'syslog' was not declared in this scope
The 2nd approach I tried (this works in Arduino IDE), was creating a small SyslogHelper.h
file, which created the extern Syslog syslog;
object, and then include this helper file in all my private libs, like so:
src/main.cpp:
#include "SyslogHelper.h"
#include <WiFiUdp.h>
WiFiUDP SyslogUDP;
void setup()
{
syslog = Syslog(SyslogUDP, "255.255.255.255", 514, "FlyballETS", "FlyballETSApp", LOG_INFO, SYSLOG_PROTO_BSD);
}
src/SyslogHelper.h:
#include <Syslog.h>
extern Syslog syslog;
lib/GPSHandler/GPSHandler.cpp:
#include "SyslogHelper.h"
void GPSHandlerClass::init(HardwareSerial * SerialPort)
{
syslog.logf_P("GPS Class initialized!");
}
When I try to compile this in PlatformIO, it fails because at the time it tries to compile SyslogHelper.h, it doesn’t seem to be aware of the location of the <Syslog.h>
file:
In file included from lib\GPSHandler\GPSHandler.cpp:7:0:
src/SyslogHelper.h:4:20: fatal error: Syslog.h: No such file or directory