Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESP8266 for Arduino IDE (xtensa-lx106-elf-gcc) and std::map linking error

Is it possible at all to use map with ESP8266 for Arduino package?

Here is my code:

#include <map>

typedef std::map<int, int> Items;

void setup() {
  Items items;
  items[2]=5;
  //items.emplace(4,5);
}

void loop() {

}

Here is compilation/linking erorrs:

Arduino: 1.6.5 (Windows 8.1), Board: "Generic ESP8266 Module, Serial, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS)"

sketch_oct31a.cpp.o: In function `loop':
C:\Program Files (x86)\Arduino/sketch_oct31a.ino:11: undefined reference to     `std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*,     std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)'
sketch_oct31a.cpp.o: In function `_M_emplace_hint_unique<const     std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<> >':
c:\users\user\appdata\roaming\arduino15\packages\esp8266\tools\xtensa-lx106-    elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-    elf\include\c++\4.8.2\bits/stl_tree.h:1673: undefined reference to     `std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*,     std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)'
sketch_oct31a.cpp.o: In function `std::_Rb_tree<int, std::pair<int const,     int>, std::_Select1st<std::pair<int const, int> >, std::less<int>,     std::allocator<std::pair<int const, int> >     >::_M_erase(std::_Rb_tree_node<std::pair<int const, int> >*)':
c:\users\user\appdata\roaming\arduino15\packages\esp8266\tools\xtensa-lx106-    elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-    elf\include\c++\4.8.2\bits/stl_tree.h:1130: undefined reference to     `std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'
sketch_oct31a.cpp.o: In function `std::_Rb_tree_iterator<std::pair<int     const, int> >::operator--()':
c:\users\user\appdata\roaming\arduino15\packages\esp8266\tools\xtensa-lx106-    elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-    elf\include\c++\4.8.2\bits/stl_tree.h:204: undefined reference to     `std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'
sketch_oct31a.cpp.o:    (.text._ZNSt8_Rb_treeIiSt4pairIKiiESt10_Select1stIS2_ESt4lessIiESaIS2_EE29_M_get    _insert_hint_unique_posESt23_Rb_tree_const_iteratorIS2_ERS1_[_ZNSt8_Rb_treeIiSt4    pairIKiiESt10_Select1stIS2_ESt4lessIiESaIS2_EE29_M_get_insert_hint_unique_posESt    23_Rb_tree_const_iteratorIS2_ERS1_]+0x0): undefined reference to `std::_Rb_tree_increment(std::_Rb_tree_node_base*)'
sketch_oct31a.cpp.o: In function `std::_Rb_tree<int, std::pair<int const,     int>, std::_Select1st<std::pair<int const, int> >, std::less<int>,     std::allocator<std::pair<int const, int> >     >::_M_get_insert_hint_unique_pos(std::_Rb_tree_const_iterator<std::pair<int     const, int> >, int const&)':
c:\users\user\appdata\roaming\arduino15\packages\esp8266\tools\xtensa-lx106-    elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/stl_tree.h:1431: undefined reference to `std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'
c:\users\user\appdata\roaming\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\bits/stl_tree.h:1448: undefined reference to `std::_Rb_tree_increment(std::_Rb_tree_node_base*)'
collect2.exe: error: ld returned 1 exit status
Error compiling.

I took that package from here: https://github.com/esp8266/Arduino

BTW any other stuff for ESP8266 compiles OK.

Thanks in advance

like image 331
x_arrange Avatar asked Oct 31 '15 11:10

x_arrange


2 Answers

The standard ESP8266 setup does not link against libstdc++, which provides map and other std namespace code.

You'll need to edit platforms.txt in the root of the ESP8266 Arudino folder (The location of this folder depends on what was used to install it, and what OS you are using) - and add -lstdc++ to the following line:

compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig -lwps -lcrypto -laxtls

Arduino IDE only reads this file once at start, so, make sure to restart the IDE before compiling.

like image 55
Dawn Minion Avatar answered Nov 10 '22 20:11

Dawn Minion


if it still doesnt work (because libstdc++ is incomplete for your toolchain), get the source at https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/src/c++98/tree.cc and rename it to tree.cpp. This worked for me.

like image 45
phu Avatar answered Nov 10 '22 19:11

phu