arduino/ArduinoCore-sam

Linker error: undifined reference to std::__throw_length_error() when using vectors

EwingKang opened this issue · 1 comments

Whenever the C++ vector is used, linker shows error of not finding function:

sketch\vector_test.ino.cpp.o: In function `std::vector<unsigned char, std::allocator<unsigned char> >::_M_check_len(unsigned int, char const*) const':

c:\users\ewing\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\c++\4.8.3\bits/stl_vector.h:1339: undefined reference to `std::__throw_length_error(char const*)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Due (Programming Port).

To reproduce: vector_test.ino

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

std::vector<uint8_t> _test_v;

void setup() {
	SerialUSB.begin(115200);
	Serial.begin(115200);
	Serial.println("=========Start Vector test==========");

	_test_v.push_back(5);
	
	Serial.print("element1: ");
	Serial.print(_test_v[0]);
	Serial.print("size: ");
	Serial.print(_test_v.size());
	delay(2000);	
}


void loop() {
	
}

More info and workaroud: https://forum.arduino.cc/index.php?topic=319795.0

If you where using gcc or clang, you would have to use -lstdc++ in order to build C++ executable.
I can't tell you exactly the how to since I use linux, but here are some hints:
If you're using MinGW (since includes a port of GCC) there should be a way to specif it.
For what I've read, you can use gcc from Cywing. So if you're using it there should be a way to pass the argument.