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.
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
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);
}
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...