Due has missing routine and gives error when compiling.

When I compile for Due (SAM board support) I get a error for an undefined reference for code that is part of the board package. I am including .

....cpp.o: In function `std::function<void (AnimationParam const&)>::operator()(AnimationParam const&) const':

c:\users\michael\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\c++\4.8.3/functional:2463: undefined reference to `std::__throw_bad_function_call()'

It seems the STL provided with SAM has exception code present (Is this even useful without a Debugger attached?) and that it is missing. Most of the suggestions online state the library path is being incorrectly defined.

When I compile for Duo

Is that anything like a Due?

Without posting any code, you are just wasting everyone's time.

c:\users\michael\appdata\local\arduino15\packages

ARDUINO IDE 1.5 BETA was released in October, 2012.

What about using a more recent version of the IDE, perhaps IDE 1.6.5-r5, for example?

And your board is possibly a "Due" (with 32-bit ARM Cortex controller), but it is hardly a "Duo".

Corrected, I miss typed Due.

It was so simple, I didn't think code was needed, I corrected that also below.

I used the latest IDE 1.6.7 and the board manager to get the latest Due support (Arduino Sam Boards by Arduino v1.6.6)

Below is the complete sketch to reproduce the problem.

#include <Arduino.h>
#include <functional>

// due to templates, need to add some code to actual use
// the feature before it cares about the code
typedef std::function<void(void)> AnimUpdateCallback;

AnimUpdateCallback animUpdate;

void UpdateTheAnima( void)
{
  
}

void setup() {
  animUpdate = UpdateTheAnima;
}

void loop() {
  animUpdate();
}

And below is the partial compiler spew as the complete one is too large to post

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Michael\AppData\Local\Arduino15\packages" -hardware "C:\Users\Michael\Documents\Arduino\hardware" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools "C:\Users\Michael\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files (x86)\Arduino\libraries" -libraries "C:\Users\Michael\Documents\Arduino\libraries" -fqbn=arduino:sam:arduino_due_x -ide-version=10607 -build-path "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\Michael\AppData\Local\Temp\arduino_76c57fe2c4b9aac48fd95f32b264b3dd\sketch_feb26a.ino"

"C:\Users\Michael\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-gcc" -mcpu=cortex-m3 -mthumb -Os -Wl,--gc-sections "-TC:\Users\Michael\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.6\variants\arduino_due_x/linker_scripts/gcc/flash.ld" "-Wl,-Map,C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp/sketch_feb26a.ino.map"  -o "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp/sketch_feb26a.ino.elf" "-LC:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp" -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--start-group "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp/core/syscalls_sam3.c.o" "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp\sketch\sketch_feb26a.ino.cpp.o" "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp\core\variant.cpp.o" "C:\Users\Michael\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.6\variants\arduino_due_x/libsam_sam3x8e_gcc_rel.a" "C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp/core\core.a" -Wl,--end-group -lm -gcc
C:\Users\Michael\AppData\Local\Temp\build76c57fe2c4b9aac48fd95f32b264b3dd.tmp\sketch\sketch_feb26a.ino.cpp.o: In function `std::function<void ()>::operator()() const':

c:\users\michael\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\c++\4.8.3/functional:2463: undefined reference to `std::__throw_bad_function_call()'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling.

#include

Where would we find this?

functional is a STL file, which is included with the ARM platform bits.

On my system, it was located at...

C:\Program Files (x86)\Arduino\hardware\tools\arm\arm-none-eabi\include\c++\4.8.4\

Now the question is, where do I go to enter a bug?

jurs:

c:\users\michael\appdata\local\arduino15\packages

ARDUINO IDE 1.5 BETA was released in October, 2012.

That path part that has arduino15 has nothing to do with the version. Its present in all versions and its were additional board installs go to from the Board Manager. I am really not sure why its not just Arduino.

It's not clear that Arduino claims to support STL.

undefined reference to `std::__throw_bad_function_call()

Traditionally embedded systems have a difficult time support exceptions, so while there may be standard STL files included, they could end up trying to invoke pieces of run-time support that just aren't there (and perhaps cannot be implemented correctly.)

This is probably an Atmel g++ bug, rather than an Arduino bug.

You could try putting in your own implementation of the function. If everything goes right, it will probably never be called, right?

westfw:
It's not clear that Arduino claims to support STL.
Traditionally embedded systems have a difficult time support exceptions, so while there may be standard STL files included, they could end up trying to invoke pieces of run-time support that just aren't there (and perhaps cannot be implemented correctly.)

This is probably an Atmel g++ bug, rather than an Arduino bug.

You are correct, but STL normally has a define that allows you to git rid of exceptions (and live with null dereference or other possible issues) but the included one doesn't have this feature so its been specifically modified.

Many of the new 32bit embedded micros include one without exceptions at all.

Looking at the code, it seems that exceptions were removed from some pieces but not all.

I will look for a place at Atmel support site to see if this is present in there release.

as suggested by westwf this will work. In this case you are responsible for the error handling.
Looking in STL code without protective eye-gear can cause serious damage. However quick look shows, that __throw_bad_function_call() is called only when you are init'ing std::function with an empty function and try to call it.

namespace std
{
void __throw_bad_function_call()
{
Log.Error(F("STL ERROR - HALT NOW"));
}
}

#include <functional>

int boo ( int a, int b)
{
return a + b;
}

typedef std::function<int(int)> func;

void foo( func f )
{
int result = f(5);
Log.Info(F("GotResult %d"), result);
}

and then you can call

foo(std::bind(boo,std::placeholders::_1,4));

Hello all,

today i think i got the same / similar error:
iam working with an ItsyBitsy M4 - SAMD51
IDE: 1.8.9
Boards:

  • Arduino SAMD Boards 1.8.1
  • Adafruit SAMD Boards 1.5.1
.../callback_test.ino.cpp.o: In function `loop':
callback_test.ino.cpp:(.text.loop+0x8): undefined reference to `std::__throw_bad_function_call()'
collect2: error: ld returned 1 exit status

my test-sketch is as following:

// some simple tests for callback things..


// experiments with std:function
// https://stackoverflow.com/questions/14189440/c-callback-using-class-member#14189561

// fix  "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max

#include <functional>


using tCallbackFunction = std::function<void(uint8_t)>;

tCallbackFunction callmeifyoucan;


void print_something(uint8_t value) {
    Serial.print("print_something:");
    Serial.println(value);
}



void setup() {
    // ------------------------------------------
    // init serial
    // wait for arduino IDE to release all serial ports after upload.
    delay(1000);
    Serial.begin(115200);
    delay(500);
    Serial.println();

    Serial.println(F("callback_test.ino sketch."));
    Serial.println(F("minimal example for C++ Callback Functions."));

    // ------------------------------------------
    callmeifyoucan = print_something;

    // ------------------------------------------
    Serial.println(F("Loop:"));
}

void loop() {
    // nothing else to do here...
    callmeifyoucan(42);
    delay(1000);
}

if i add the function definition from ikostov like so:

// some simple tests for callback things..


// experiments with std:function
// https://stackoverflow.com/questions/14189440/c-callback-using-class-member#14189561

// fix  "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max

// this tries to fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
namespace std {
    void __throw_bad_function_call() {
        Serial.println(F("STL ERROR - __throw_bad_function_call"));
    }
}

#include <functional>


using tCallbackFunction = std::function<void(uint8_t)>;

tCallbackFunction callmeifyoucan;


void print_something(uint8_t value) {
    Serial.print("print_something:");
    Serial.println(value);
}



void setup() {
    // ------------------------------------------
    // init serial
    // wait for arduino IDE to release all serial ports after upload.
    delay(1000);
    Serial.begin(115200);
    delay(500);
    Serial.println();

    Serial.println(F("callback_test.ino sketch."));
    Serial.println(F("minimal example for C++ Callback Functions."));

    // ------------------------------------------
    callmeifyoucan = print_something;

    // ------------------------------------------
    Serial.println(F("Loop:"));
}

void loop() {
    // nothing else to do here...
    callmeifyoucan(42);
    delay(1000);
}

this results in a warning:

.../callback_test.ino: In function 'void std::__throw_bad_function_call()':
.../callback_test.ino:18:5: warning: 'noreturn' function does return [enabled by default]
     }
     ^

how to fix this?
i don't see somethin returning there...

sunny greetings
stefan

Looks like you should do:

namespace std {
    void __throw_bad_function_call() {
        Serial.println(F("STL ERROR - __throw_bad_function_call"));
        __builtin_unreachable();
    }
}
[url=https://stackoverflow.com/questions/25408795/tell-gcc-that-a-function-call-will-not-return]https://stackoverflow.com/questions/25408795/tell-gcc-that-a-function-call-will-not-return[/url]