I'm conversant with C++ and I've used g++ on handfuls of platforms. The g++ in the Arduino IDE has a weird bug I haven't seen elsewhere.
This code compiles fine:
#include <SPI.h> //to drive the dotstar strips
#include <Adafruit_FreeTouch.h> //touch sensing
#include <cassert>
#include <cstdint>
#define DEBUGGING //
#define NUMOF(x) (sizeof (x) / sizeof (x[0]))
enum DeviceState : uint8_t {
S_STANDBY, S_STANDBY_TRIGGERED, S_ARM, S_RAMPUP, S_FIRING,
S_RAMPDOWN
};
static DeviceState state;
static bool looping = false; //so we don't call serial from setup()
static void debugPrint(const char* tx)
{
#ifdef DEBUGGING
//Make sure we don't call Serial.print until loop() happens.
//It seems to make a mess when called from setup().
if (looping)
{
Serial.print(tx);
}
#endif
}
//...followed by many uses of DeviceState
But if I move the enum declaration to after the definition of debugPrint, all subsequent uses of DeviceState come up as undefined. This is true even if I change the name of DeviceState to something else, or make it into a typedef uint8_t DeviceState; and use #define or static const to create the S_ constants. In short, this fails:
#include <SPI.h> //to drive the dotstar strips
#include <Adafruit_FreeTouch.h> //touch sensing
#include <cassert>
#include <cstdint>
#define DEBUGGING //
#define NUMOF(x) (sizeof (x) / sizeof (x[0]))
static bool looping = false; //so we don't call serial from setup()
static void debugPrint(const char* tx)
{
#ifdef DEBUGGING
//Make sure we don't call Serial.print until loop() happens.
//It seems to make a mess when called from setup().
if (looping)
{
Serial.print(tx);
}
#endif
}
enum DeviceState : uint8_t {
S_STANDBY, S_STANDBY_TRIGGERED, S_ARM, S_RAMPUP, S_FIRING,
S_RAMPDOWN
};
static DeviceState state; //no error on this line...
//...followed by many uses of DeviceState, all of which claim that DeviceState does not name a type
There's certainly nothing in C++ that demands all enums, typedefs etc be placed before functions. Obviously I can live with the enum declared earlier, but I'm fussy about my code and I generally define things in the order of need, and debugPrint() doesn't need DeviceState so it should be above DeviceState's definition. My concern, obviously, is if something as simple as declaring a type can break g++, there's something very wrong with the compiler and if you can't trust a compiler you shouldn't let it compile code that controls hardware.
Anyone seen anything like this? Any known fix? I'm up to date on Arduino IDE and am on Linux Mint.