Arduino C++ code: can you use virtual functions and exceptions?
Asked Answered
C

6

16

Following up on this comment from the question Writing firmware: assembly or high level?:

When compiling C++ code for the Arduino platform, can you use virtual functions, exceptions, etc? Or would you want to (have to) use a subset of C++ (as described in the comment)?

Any other caveats when programming for the Arduino platform?

Cleisthenes answered 20/1, 2009 at 15:44 Comment(0)
C
15

The Arduino environment uses the AVR version of the GCC toolchain. The code is compiled as C++, so you can use classes. Virtual functions are possible; the vtables will be stored in the .data section and have the correct addresses. In fact, the Print base class uses virtual functions to adapt the various "print" methods to the different output types.

Exceptions are not supported because of code space reasons. The Arduino environment passes "-fno-exceptions" to the compiler command line. See the source for verification of this.

Templates are supported. For example, this no-cost stream insertion operator technique works fine using a simple template and inline operator.

Calibre answered 7/4, 2009 at 22:23 Comment(0)
B
12

The Arduino software uses avr-gcc to compile sketches. The following limitations were sourced from the avrlibc FAQ (Can I use C++ on the AVR?):

Supported

  • Virtual functions
  • Constructors and destructors (including global ones)

Not supported

  • C++ standard functions, classes, and template classes (but see this port of uClibc++ for Arduino)
  • operators new and delete (attempting to use them will cause the linker to complain about undefined external references). This means that objects can only be created on the stack. If dynamic memory allocation is required it must be implemented using malloc() and free() of C types
  • Exceptions. Since exceptions are enabled by default in the C++ frontend, they explicitly need to be turned off using -fno-exceptions in the compiler options. This is done automatically by the Arduino IDE when it launches avr-gcc

Other issues

  • Some of the supplied include files are not C++ safe, i.e. they need to be wrapped with
    extern "C" { . . . }
Bashkir answered 17/6, 2009 at 13:2 Comment(5)
Do you have a source for the "new" operator not being supported? I use it in a sketch and it works just fine and compiles without any warning.Fulgurant
@Fulgurant Interesting. I've referenced the avrlibc FAQ linked at the top of my answer. That FAQ suggests that the new limitation could perhaps be fixed so maybe it has been and the FAQ is now out of date?Bashkir
I would guess the FAQ is out of date. There's an Arduino tutorial on how to use classes for libraries ( arduino.cc/en/Hacking/LibraryTutorial ) and I'm using abstract classes and virtual functions in my current sketch.Fulgurant
@Fulgurant Classes have always been OK as long as instances were created globally or on the stack (i.e. not on the heap using new - which is what the LibraryTutorial does). And the referenced FAQ says that virtual functions are OK too.Bashkir
Outdated answer, current Arduino supports new/delete, templates, C++ classes.Currie
C
3

The usability of a features is not limited by the platform but rather the compiler that you are using.

I would check your compiler documentation on what language features are supported.

Coincidentally answered 21/1, 2009 at 4:38 Comment(8)
Not quite...constrained hardware has a major role here in deciding if exceptions should be supported.Animated
It is not a constraint of the hardware. It is a constrainted imposed on the compiler because we think it best not to overload the hardware (the hardware could theoritically support it but we dont want to use the required resources to do so).Coincidentally
Any constraints on the compiler are likely to impose further constraints on the programs being compiled. The hardware could theoretically support it but only as long as your own resource demands do not exceed a certain limit. Due to the nature of the problem this limit is quite unstable (changes with your program complexity/requirements) hence its not prudent to switch exceptions ON/OFF based on this (will cause major code churn). Evidently there exists some hardware X which cannot even support basic exception handling on its own making your "constraint on compiler" argument moot.Animated
You are missing the point. This is not a hardware constraint it is a constrain imposed by the compiler. Also do you have a reference to such hardware that can not support exceptions?Coincidentally
I am not trying to be difficult but I would really like to understand how this is a compiler problem. All I am saying is, is there a reason why the compiler is constrained in the first place? Is it not because the hardware can barely cope with the application logic without exceptions and adding exceptions is just going to kill it (in all probability)?Animated
I hate to dredge this one up, but is there anything "special" about exceptions. Is there special hardware features required to support exceptions? For instance, if I hacked the arduino environment to not pass the -fnoexceptions flag, are there any hardware requisites (excluding resources like memory size, etc) that my board could potentially lack? Or are exceptions intrinsic to the machine code?Brigidbrigida
@aaaidan: Nope, The arduino environment could theoretically handle exceptions, but programs would take significantly more space. (2x or 3x?)Amelia
Any idea how to enable them? #10096091Brigidbrigida
K
2

Comeau computing has a C++ to C compiler which supports all C++ features. Not just virtual functions, or exceptions, but also export. You would need to call Comeau to get it to target the Arduino language, but should be not too hard.

Kantar answered 22/1, 2009 at 14:29 Comment(0)
F
1

The documentation says:

The Arduino language is based on C/C++ and supports all standard C constructs and some C++ features.

It doesn't say anything about virtual functions that I can find.

I tried a simple program and it compiles fine. I haven't tested it on Arduino hardware though.

EDIT: I also haven't tested exceptions.

Fadden answered 21/1, 2009 at 6:46 Comment(0)
Y
1

I used g++ with this Makefile and started with this code. C++ wastes some space. But protothreads are too cool not to use.

Yearning answered 3/3, 2009 at 18:5 Comment(1)
Makefile is no longer availableEggnog

© 2022 - 2024 — McMap. All rights reserved.