7

Behold this insanity:

enum WhatArduinoIs {
  Easy, But, Insane, Obsolete, And, Far, Worse, Than, mBed
};

void TellMe(WhatArduinoIs pls) { }

void setup() { }
void loop() { }

I'm pretty good at C++, so I'm fairly sure that should compile. However I get this error:

sketch_jan21b.ino:3:13: error: variable or field 'TellMe' declared void
sketch_jan21b.ino:3:13: error: 'WhatArduinoIs' was not declared in this scope
Error compiling.

I've encountered the Arduino "IDE"'s propensity for insane and totally unrobust modification of programs before. Specifically when you #include libraries Arduino only magically adds the relevant C++ files if the #include is in your main sketch. #include <LiquidCrystal.h> in another C++ file? Link errors.

I suspect in this case it is Arduino magically trying to add function declarations to the start of the file so their order doesn't matter. I.e. it turns the file into this:

void TellMe(WhatArduinoIs pls);

enum WhatArduinoIs {
  Easy, But, Insane, Obsolete, And, Far, Worse, Than, mBed
};

void TellMe(WhatArduinoIs pls) { }

void setup() { }
void loop() { }

That obviously doesn't work. How can I tell Arduino to leave my damn program alone?

6

You can workaround the issue by putting your enum and function into a namespace. You can even take advantage of C++ unnamed namespaces if you like:

namespace
{
  enum WhatArduinoIs {
    Easy, But, Insane, Obsolete, And, Far, Worse, Than, mBed
  };

  void TellMe(WhatArduinoIs pls) { }
}

void setup() { }
void loop() { }

It's annoying (as are many things in the Arduino IDE), but it seems to work.

If you're more experienced with programming then you might want to consider using a 3rd party IDE with an Arduino plugin, such as Eclipse. The Arduino IDE is really only designed for beginners.

  • Or even something like Geany with (or without) Ino. – Ignacio Vazquez-Abrams Jan 21 '15 at 17:01
  • Thanks! I was going to say the only thing worse than Arduino's IDE is Eclipse, but I think actually I think even Eclipse might be better! I strongly prefer Qt Creator - I use that for mBed. I may have to finally set up Arduino in it. The problem is many details of Arduino's build system are hidden in Arduino.exe so it is a pain to set up any other IDE. Didn't know Eclipse had an Arduino plugin though. – Timmmm Jan 22 '15 at 10:09
  • There is also the free Atmel Studio Ide. It supports professional programming features like multiple source files, SVN and the AVR dragon debugger. – kiwiron Jan 22 '15 at 18:39
  • This is something that is getting fixed. Try the IDE linked here github.com/arduino/Arduino/pull/2729. The sketch will compile just fine. – Federico Fissore Mar 9 '15 at 7:20
  • Or you could just use a plain Makefile. There are a couple of generic Arduino Makefiles floating around, that do almost all the IDE does, except messing with your source. – Edgar Bonet Mar 9 '15 at 8:43
4

You seem to be correct about what the arduino IDE is doing to your sketch.

Arduino will not preprocess any .h or .cpp files, so you could use a second file. you could also break the IDE's function locating regex with a dummy throw() statement like this:

void TellMe(WhatArduinoIs pls) throw() { }

The dummy throw allows it to compile for me on 1.5.8

1

All you need is a function prototype. I have a detailed explanation at Classes and objects: how many and which file types I actually need to use them?

So, this compiles:

enum WhatArduinoIs {
  Easy, But, Insane, Obsolete, And, Far, Worse, Than, mBed
};

void TellMe(WhatArduinoIs pls);  // prototype
void TellMe(WhatArduinoIs pls) 
  {
  }
void setup() { }
void loop() { }

Off-site reference: How to avoid the quirks of the IDE sketch file pre-preprocessing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.