Jump to content
 

SAMD21 Arduino and the C++ STL


N9WXU
 Share

Recommended Posts

  • Member

This is a quick note to warn everyone using the SAMD21 in an Arduino environment with the STL.  I use C++ all the time and much of the power comes from using the STL.  Vector, queue. array, etc are all canned, powerful libraries that make your life easier.

However, the Arduino runtime environment for the SAMD21 conflicts with these libraries.  Here is a quick example:


#include <Arduino.h>
#include <vector>

using namespace std;

vector<int> count;

void setup() {
  Serial.begin(115200);
  count.push_back(0);
}

void loop() {
  Serial.println("Hello");
  delay(500);
}

A trivial use of a vector to burn up all the RAM.  Simply including the vector causes a conflict with the implementation of min(a,b)

Building in release mode
Compiling .pio/build/mkrzero/src/main.cpp.o
In file included from /Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/vector:60:0,
                 from src/main.cpp:3:
/Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
                                                        ^
/Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:265:56: error: macro "max" passed 3 arguments, but takes just 2
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
                                                        ^
In file included from src/main.cpp:2:0:
/Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected unqualified-id before 'const'
     min(const _Tp& __a, const _Tp& __b)
     ^
/Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected ')' before 'const'
/Users/joejulicher/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected ')' before 'const'

The implementation of min for Arduino conflicts with the implementation for the STL.  I have done this same build with ESP32 and STM32 and did not have these conflicts.  Therefore these are SAMD21 "features".

Fortunately you can easily work-around the issue by reversing the include order.  Put vector (or other STL's) before the Arduino.h and it compiles just fine.  Of course I have not exhaustively tested this combination so your milage may vary.

Good Luck

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

 


×
×
  • Create New...