STL and undefined reference to std __throw_bad_alloc()

Status
Not open for further replies.

truth

Member
Visual Studio 2017 Professional with all the latest updates
Visual Micro IDE Extension (latest update: 1801.27.0)
Arduino IDE 1.8.5
Teensyduino 1.41

IDE: Arduino 1.6/1.9
Board: Teensy 3.6

We have followed the solution provided here (https://forum.pjrc.com/threads/23467-Using-std-vector?p=69787&viewfull=1#post69787) and it has been working for months but now is failing with:

Code:
// Adding functions not included in std for compiler
#pragma once
extern "C"
{
	int _getpid() { return -1; }
	int _kill(int pid, int sig) { return -1; }
}
namespace std {
	void __throw_bad_alloc()
	{
		Serial.println("Unable to allocate memory");
	}

	void __throw_length_error(char const*e)
	{
		Serial.print("Length Error :");
		Serial.println(e);
	}
}
Code:
Compiling 'Manifold' for 'Teensy 3.6'
 
City.cpp.o*: In function __gnu_cxx::new_allocator<CanKingdomStuff::Letter>::allocate(unsigned int, void const*)
new_allocator.h:102: undefined reference to std  __throw_bad_alloc()

Error linking for board Teensy 3.6
new_allocator.h:102: undefined reference to std  __throw_bad_alloc()
Build failed for project 'Manifold'
 
Folder.cpp.o*: In function std::vector<CanKingdomStuff::Letter, std::allocator<CanKingdomStuff::Letter> >::reserve(unsigned int)
vector.tcc:69: undefined reference to std  __throw_length_error(char const*)
 
Page.cpp.o*: In function std::vector<unsigned char, std::allocator<unsigned char> >::_M_check_len(unsigned int, char const*) const
stl_vector.h:1425: undefined reference to std  __throw_length_error(char const*)
 
collect2.exe*: error: ld returned 1 exit status
 
Does this problem happen with only the Arduino IDE and Teensyduino? (not using visual studio)

Actually, it failed in the Arduino IDE because it didn't find <StlHelper.h>, our include file with the 'missing' STL code.

The fix was:
1. Change <StlHelper.h> to "StlHelper.h" (but why? it was working for Teensy 3.1!!!)
2. Update boards.txt with the correct link flags for board 3.6 (we had only updated for 3.1)

A bit convoluted but it is working now.:cool:
 
Always difficult to answer "why", sometimes even when I have all the code in Arduino and I can click Verify to see the problem.

But I can tell you some time ago we upgraded the gcc toolchain Teensyduino uses within Arduino from 4.8 to 5.4. Maybe your earlier results with Teensy 3.1 were using a much older version which had the gcc 4.8 compiler? However, that's just a guess, based on not actually seeing the code, not actually knowing exactly which versions of the software you used.
 
you can just add this to your sketch, it'll get rid of your errors (tested on Teensy 3.6):
I had the same errors when I used STL containers, this ressolves it.

Code:
namespace std {
void __throw_bad_alloc() {
  Serial.println("Unable to allocate memory");
}
void __throw_length_error( char const*e ) {
  Serial.print("Length Error :"); Serial.println(e);
}
}
 
I'm going to add some new information because I think it might be useful to those using PlatformIO. I'm getting an "undefined reference to _write" linker error which turns out to be the same as "undefined reference to std::__throw_bad_alloc()". In the Arduino IDE (including the latest 1.49 beta build of Teensyduino.app), it shows up as "std::__throw_bad_alloc()", but in PlatformIO, it shows up as "_write".

First, the program:
Code:
#include <Arduino.h>

// C++ includes
#include <queue>
#include <vector>

std::queue<std::vector<uint8_t>> msgs;

void setup() {
  uint8_t b[4]{0x01, 0x02, 0x03, 0x04};
  msgs.emplace(&b[0], &b[4]);
  // std::vector<uint8_t> v(&b[0], &b[4]);
  // msgs.push(v);
}

void loop() {
}

The linker error:
Code:
Linking .pio/build/teensy31/firmware.elf
/Users/me/.platformio/packages/toolchain-gccarmnoneeabi@1.50401.190816/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m/libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x12): undefined reference to `_write'

The linker error in the Arduino IDE:
Code:
/Applications/Arduino-1.8.10.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/ext/new_allocator.h:102: undefined reference to `std::__throw_bad_alloc()'

When I add the following code to the program, the link completes successfully.
Code:
// This is so STL things link.
namespace std {
void __throw_bad_alloc() {
  // Serial.println("Unable to allocate memory");
  while (true) {}
}
}

This might help those searching for "undefined reference to _write" as the error.
 
Status
Not open for further replies.
Back
Top