Enum class and C++11

#1

I am getting compiler errors when trying to compile code with a C++11 enum class. At first I assumed this was because the -std=c++11 flag was not being passed into the compiler, so I tried adding it to the cxxflags in mos.yml, but no luck with that either.

The code which throws the error is:

enum class interrupt_e
{
    Global,
    Radio
};

I am building locally with the following command

mos build --verbose --local

In the mos.yml file I have added this, but without any change to the compilation result

cxxflags:
  - "-std=c++11"

The errors the compiler throws are

14:1: error: expected identifier before 'enum'
 enum class interrupt_e
 ^~~~
 18:1: error: multiple types in one declaration
 };
^

Is C++11 supported by the MGOS build system?

#2

By default mos builds for gnu++11 which is C++11 with some extensions and disable exceptions.
Is your enum class interrupt_e definition in cpp file, or included in a cpp file?

This code compiles without error for esp8266, esp32 and ubuntu platforms without any extra flags in my main.cpp

#include "mgos.h"

enum class interrupt_e {
  Global,
  Radio,
};

static void timer_cb(void *arg) {
  interrupt_e *e = static_cast<interrupt_e *>(arg);
  static bool s_tick_tock = false;
  LOG(LL_INFO, ("%s uptime: %.2lf, heap free/min_free: %zu/%zu, e: %d",
                (s_tick_tock ? "Tick" : "Tock"), mgos_uptime(),
                mgos_get_free_heap_size(), mgos_get_min_free_heap_size(),
                static_cast<int>(*e)));
  s_tick_tock = !s_tick_tock;
}

extern "C" enum mgos_app_init_result mgos_app_init(void) {
  interrupt_e *e = new interrupt_e();
  *e = interrupt_e::Radio;
  mgos_set_timer(10000, MGOS_TIMER_REPEAT, timer_cb, e);
  return MGOS_APP_INIT_SUCCESS;
}
#3

Thank you, that eliminates the C++ version as an issue. This is in a header file. I’ll take a look at this again and see if I can figure it out

#4

Sorted! I had a dangling “class” at the end of one of the header files. :man_facepalming:

Thanks for the kick in the right direction.