1

I want to use c++ data structures such as vectors, arrays, map... in my Arduino sketch. I made sure to add -std=c++11 and -std=gnu++11 in platform.txt, I am still getting the following error message:

fatal error: unordered_map: No such file or directory #include <unordered_map> compilation terminated. exit status 1 unordered_map: No such file or directory

Here's a snapshot from ~/arduino-nightly/hardware/arduino/avr/platform.txt:

enter image description here

Versions:

  • Arduino ide: 1.8.14
  • g++: 5.4.0
  • avr-g++: 4.9.2

What else should I set up?

1 Answer 1

3

The AVR-based Arduinos don't come with anything close to a full C++ standard library implementation. They have avr-libc which is fairly complete to at least the C99 standard, that is the C standard from 1999, not C++. What you're doing with -std=c++11 is influencing the standard used by the compiler to interpret the C++ language itself. This does work fine on AVR based Arduinos, but is not going to give you the sort of C++ containers you may be used to on a desktop or larger embedded system, like an ESP8266.

Much of the C++ standard library would be completely out of place on an AVR based Arduino, particularly those parts that make use of dynamic allocation like unordered_map in your error message.

There are some parts of the C++ library implemented via Arduino STL. Whether or not you should be using containers from it is another question.

If you do try to use Arduino STL, I'd encourage you to use more conservative parts of it. Something like an unordered_map, but with less (or no) dynamic allocation could be implemented by using Arduino STL's std::array and functions from its <algorithm> header, like their std::find or std::lower_bound, etc. Trying to use a C++ map or vector container or something of that sort is kind of asking for trouble, more so on the more common AVR-based Arduinos with only roughly 2 Kbyte of RAM.

1
  • Thanks for the answer. Could you show me a way to use std::array in arduino (I am having trouble #including<array>)
    – S.E.K.
    Commented Mar 18, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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