Arduino C++ code: can you use virtual functions an

2019-03-09 22:20发布

问题:

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?

回答1:

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.



回答2:

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" { . . . }


回答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.



回答4:

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.



回答5:

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.



回答6:

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