platformio / platformio-core

Your Gateway to Embedded Software Development Excellence :alien:
https://platformio.org
Apache License 2.0
7.93k stars 792 forks source link

Linker Error When Using <functional> #2363

Closed Zackh1998 closed 5 years ago

Zackh1998 commented 5 years ago

I've reduced a linker error in one of my projects to the following piece of code:

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

void setup()
{
   // Serial.println();
  std::function<void()> func;
  func();
}

void loop() {}

When I try to compile and link this, it compiles fine, but fails to link. If i uncomment the Serial.println(), then it links and compiles.

I am using the following settings in the platformio.ini file:

[env:teensy35]
platform = teensy
board = teensy35
framework = arduino

The error it gives when failing to link is:

c:/users/zackh/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m/fpu\libc.a(lib_a-writer.o): In function _write_r': writer.c:(.text._write_r+0x12): undefined reference to_write' collect2.exe: error: ld returned 1 exit status

Any idea what's happening here?

Zackh1998 commented 5 years ago

It might be worth noting that the following code compiles, links, and actually runs and produces expected output on a teensy board:

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

void test() {
  Serial.println("test"); 
}

void setup()
{  
}

void loop() {
  std::function<void()> _test = test;
  test();
}
Zackh1998 commented 5 years ago

Hmm, it seems that adding the following line in platformio.ini fixed it.

build_flags = -llibc -lc