Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: STL and undefined reference to std __throw_bad_alloc()

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    6

    STL and undefined reference to std __throw_bad_alloc()

    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...ll=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

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Quote Originally Posted by truth View Post
    IDE: Arduino 1.6/1.9
    Board: Teensy 3.6
    Does this problem happen with only the Arduino IDE and Teensyduino? (not using visual studio)

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    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.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    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.

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,070
    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);
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •