Undefined reference to `_write'

Could someone please advise/help with the following build problem...

The code below (.ino file, to randomly fade a small bunch of LEDs on an old Teensy LC) gives the following error when compiling or linking...

/home/peter/.arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /home/peter/.arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc_nano.a(libc_a-writer.o): in function `_write_r':
(.text._write_r+0x10): undefined reference to `_write'
collect2: error: ld returned 1 exit status
exit status 1

Compilation error: exit status 1

Code (.ino file)

C:
// Randomly fade a bunch of LEDs


// Create an IntervalTimer object
IntervalTimer frameTimer;

#define LEDS 10  // number of LEDs handled
byte led = 0;

#define FRAME_RAT 50                                  // main frame-rate in Hz
#define FRAME_PER (1000000 / FRAME_RAT)               // frame-period in µs
#define FADE_FMS(led) (fadeTIM[led] * FRAME_RAT / 2)  // number of frames in fade(of ½s-units)
#define FADE_RTO(led) (FRAME_PER / FADE_FMS(led))     // per frame fraction per./no. of frames

bool fadeENB[LEDS] = { false };  // true = enable, false = disable
uint8_t fadePIN[LEDS] = { LED_BUILTIN };  // digital-pin assignments
uint8_t fadeTIM[LEDS] = { 0 };   // max. 50 ½-sec units (25 s)
uint8_t fadeBNS[LEDS] = { 0 };   // max 31 3% steps
bool fadeUP[LEDS] = { false };   // true = up, false = down
int frameCNT[LEDS] = { 0 };      // a fade's frame count

void pulseLED(void) {
  // start of a new frame
  // find inactive LEDs

  // temporarily work with one LED (led = 0 - initialised value)
 // for ( uint8_t led = 0; led < LEDS; led++)
  {
    // if LED inactive then randomly enable and set it up
    if (!fadeENB[led]) {
      randomSeed(analogRead(0));
      fadeENB[led] = random() & 1;     // true = enable, false = disable
      if (fadeENB[led]) {
        fadeTIM[led] = random(1, 50);  // max. 49 ½ sec units (25 s)
        fadeBNS[led] = random(1, 64);  // max 63 1½ % steps
        fadeUP[led] = true;            // true = up, false = down
        frameCNT[led] = 0;             // a fade's frame count
      } else {
        fadeTIM[led] = 0;     // max. 49 ½ sec units (25 s)
        fadeBNS[led] = 0;     // max 63 1½ % steps
        fadeUP[led] = false;  // true = up, false = down
        frameCNT[led] = 0;    // a fade's frame count
      }
      // Serial.println(fadeTIM[led]);
    }
    else { // drive those still actively fading
    // for ( uint8_t led = 0; led < LEDS; led++) {
      // when fading-up finsihed, start fading-down
      if (frameCNT[led] >= FADE_FMS(led)) fadeUP[led] = false;
      else {
        if (frameCNT[led] <= 0) {
          fadeUP[led] = true;
          fadeENB[led] = false;
        }
      }
      // start led pulse
      digitalWrite(fadePIN[led], HIGH);
      // define pulse width
      delayMicroseconds(FADE_RTO(led) * frameCNT[led] * fadeBNS[led] / 63);
      // drive direction of fading
      fadeUP[led] ? frameCNT[led]++ : frameCNT[led]--;
      digitalWrite(fadePIN[led], LOW);
      // Serial.println(FADE_FMS(led));
    }
  }
}

// the setup function runs once when you press reset or power the board
void setup() {
  // Serial.begin(9600);
  pinMode(fadePIN[led], OUTPUT);
  frameTimer.begin(pulseLED, FRAME_PER);  // pulseLED at 20 ms rate
}

// the loop function runs over and over again forever
void loop() {
  while (true)
    ;
}

Environment
Running Arduino Version: 2.3.6 under Linux Manjaro on Lenovo T420 (working pressure: 7 bar)

My many thanks in advance to The Forum

Peter
 
Thought I remembered this - it's a problem that's been reported before :)

Adding the following to the end of your sketch fixes the link error:
C++:
extern "C"
{
  __attribute__((weak))
  int _write(int file, char *ptr, int len)
  {
      return 0;
  }
}

Curious as to why you've done this:
Code:
// the loop function runs over and over again forever
void loop() {
  while (true)
    ;
}

loop() effectively is a while (true) {}, but adds some essential housekeeping. Of course, if the housekeeping interferes with your code, that'd be the explanation, but a comment to that effect wouldn't be amiss.
 
Thought I remembered this - it's a problem that's been reported before :)

Adding the following to the end of your sketch fixes the link error:
C++:
extern "C"
{
  __attribute__((weak))
  int _write(int file, char *ptr, int len)
  {
      return 0;
  }
}

Curious as to why you've done this:
Code:
// the loop function runs over and over again forever
void loop() {
  while (true)
    ;
}

loop() effectively is a while (true) {}, but adds some essential housekeeping. Of course, if the housekeeping interferes with your code, that'd be the explanation, but a comment to that effect wouldn't be amiss.
Many thanks for the very prompt reply and help! The dummy stub should/would indeed fix it.

Meanwhile, through a process of removing/returning source-lines, removing line 32 viz. fadeENB[led] = random() & 1; removed the error.
Changing this line to fadeENB[led] = random(2); is accepted without error. However, in case it wants to appear again, the dummy stub is the answer.

The while (true); is accidental, a remnant. The 'randomising' process was previously in this loop, with the latter thought necessary.
To avoid visual discontinuities, it was thought better to manage fading at the front-end of a frame.
I am a retired electronics engineer with little (and ancient) formal SW training.
 
Back
Top