Go Down

Topic: Notes on STL implementations for Arduino / AVR (Read 3370 times) previous topic - next topic


I was trying out various implementations of the C++ STL libraries with Arduino 1.6.6 and figured it might be useful to post my notes here. Anyone have any experience or thoughts on these?

(See also http://www.gammon.com.au/forum/?id=11119)




This is a modification of stlport to AVR.
Diff: https://github.com/vancegroup/stlport-avr/compare/408d252b2032fb80ae58a9325cbe2363d1e3f60c...STLport-master-avr

Almost up-to-date w/ STLPort 5.2 (see: http://sourceforge.net/p/stlport/code/ci/6ecc702c2bc841ef197a9943b1510e23da7b6246/log/?path=)

(Apparently, STLPort is an updated version of the SGI STL used by Andy Brown's AVR STL below, so this may be more robust / modern than that one.)



Port of uClibc++ (http://cxx.uclibc.org) to Arduino. Seems to work with Arduino 1.6.6.
Also see: https://github.com/rpavlik/StandardCplusplus

Andy Brown's AVR STL


SGI STL + uSTL's streams. Works in Arduino 1.6.6 w/ minor patches:

Code: [Select]
diff --git a/pnew.cpp b/pnew.cpp
index 5d1c6a0..b96b62c 100755
--- a/pnew.cpp
+++ b/pnew.cpp
@@ -9,6 +9,8 @@
  * Global placement operator new

+#include <cstddef>
 void* operator new(size_t size_,void *ptr_)
        return ptr_;
diff --git a/stl_vector.h b/stl_vector.h
index 18e1bc4..19dde40 100755
--- a/stl_vector.h
+++ b/stl_vector.h
@@ -114,7 +114,7 @@ struct _Vector_base
     _Base::_M_end_of_storage = _Base::_M_start + __n;

-  ~_Vector_base() { _M_deallocate(_Base::_M_start, _Base::_M_end_of_storage - _Base::_M_start); }
+  ~_Vector_base() { this->_M_deallocate(_Base::_M_start, _Base::_M_end_of_storage - _Base::_M_start); }




A port of an old version of uSTL to avr. Doesn't compile on Arduino 1.6.6. With this code:

Code: [Select]
#undef max
#undef min

typedef long ssize_t;
typedef long off_t;

#include <ustl.h>

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

I get:

In file included from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/uutility.h:14:0,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ualgobase.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/cmemlink.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/memlink.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/memblock.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/uvector.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/uspecial.h:9,
                 from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ustl.h:9,
                 from /Users/mellis/Documents/Arduino/avrstl-test/avrstl-test.ino:7:
/Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ulimits.h:91:15: error: expected unqualified-id before 'alignof'
 inline size_t alignof (const T&)
/Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ulimits.h:100:31: error: expected unqualified-id before 'alignof'
     template <> inline size_t alignof (const type&) { return (grain); } }
/Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ustring.h:266:1: note: in expansion of macro 'ALIGNOF'
 ALIGNOF (ustl::string, alignof (string::value_type()))
In file included from /Users/mellis/Documents/Arduino/libraries/avrstl-read-only/ustl.h:9:0,
                 from /Users/mellis/Documents/Arduino/avrstl-test/avrstl-test.ino:7:
/Users/mellis/Documents/Arduino/libraries/avrstl-read-only/uspecial.h:114:15: error: expected unqualified-id before 'alignof'
 inline size_t alignof (const vector<T>&)
/Users/mellis/Documents/Arduino/libraries/avrstl-read-only/uspecial.h:155:15: error: expected unqualified-id before 'alignof'
 inline size_t alignof (const tuple<N,T>&) { return (alignof (NullValue<T>())); }
avrstl-test:9: error: 'namespaceustl' does not name a type
 void setup() {}
exit status 1
'namespaceustl' does not name a type

Not sure what's going on.



Seems to be well-maintained but doesn't run on AVR. At least, not easily.



Looks incomplete / unfinished.


Looks like stlport-avr doesn't include serial streams, so I might try StandardCplusplus for now, which does.



I struggled for a while trying to get Andy Brown's STL to work, but was never able to succeed. Ditto for stlport. Maybe I didn't follow the instructions correctly? Maybe those ports suffer from bit rot?

I finally struck gold with StandardCplusplus. I only ran into one unpleasant surprise: The instructions do not mention it, but you need to #include <StandardCplusplus.h>. I sent them a pull request to update their README with this information.

I'm sure you can get pretty far without STL, but as a professional software engineer (who doesn't work on embedded), I find it hard to imagine living without vector and/or map. IMHO, Arduino should really ship with these (or perhaps simplified replacements)! I wonder why it doesn't?

Go Up