Go Down

Topic: Error compiling my custom library for Teensy LC (Read 97 times) previous topic - next topic

kschiang19

Hi all,
This is my first time using an Arduino and writing a library for my sketch. I'm using the FastLED library as well and I wanted to write my own library to abstract away the need to manually change which LEDs to write up based what I want to show. I've been using YouTube tutorials to guide me but I'm having trouble compiling my code for my board. I'd really appreciate it if someone could help me understand what the error codes mean.

Basic sketch:
Code: [Select]

#include "FastLED.h"
#include "test.h"

test display(false);

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  display.displayModel('A');
}


Library cpp file:
Code: [Select]

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

#include "FastLED.h"
#include "test.h"

using namespace std;

test::test(bool displayMsg) {
}

void test::displayModel(char value) {
  int i, loc;
  char *binary;
  getValue(value, binary);
  for (i = 0; i < 35; ++i) {
    if (binary[i] == '1') {
      leds[i] = CRGB::Red;
    } else {
      leds[i] = CRGB::Black;
    }
  }
  FastLED.show();
}

void test::getValue(char target, char *binary) {
  string strChar; string strBinary;
  char value;
  ifstream input("input.csv");
  while (getline(input, strChar, ',') && getline(input, strBinary, '\n')) {
    value = strChar[0];
    if (value == target) {
      break;
    }
  }
  strcpy(binary, strBinary.c_str());
  input.close();
}


Header file:
Code: [Select]

#ifndef test_h
#define test_h

#if (ARDUINO >= 100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

#define NUM_LEDS 35
#define DATA_PIN 17

CRGB leds[NUM_LEDS];

class test {
  public:
 
    test(bool displayMsg=false);

    //Sets and displays the desired letter and its location
    void displayModel(char value);

  private:
   
    //Copies binary value to an array
    void getValue(char value, char *binary);
};

#endif


Error messages:
Code: [Select]

/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp.o:(.bss.leds+0x0): multiple definition of `leds'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/test.ino.cpp.o:(.bss.leds+0x0): first defined here
/Applications/Arduino.app/Contents/Java/hardware/tools/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld: Disabling relaxation: it will not work with multiple definitions
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp.o: In function `testMDL::getValue(char, char*)':
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:32: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:36: undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char)'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:36: undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char)'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:43: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::close()'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:32: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp.o: In function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()':
/Applications/Arduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/bits/basic_string.h:559: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose()'
/Applications/Arduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/bits/basic_string.h:559: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose()'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp.o: In function `__static_initialization_and_destruction_0':
/Applications/Arduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/c++/5.4.1/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp.o: In function `_GLOBAL__sub_I_leds':
/var/folders/h2/_2ysxrn572v4h05vw8xhf_640000gn/T/arduino_build_835715/sketch/testMDL.cpp:83: undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Error compiling for board Teensy LC.

PieterP

#1
Sep 14, 2018, 01:57 pm Last Edit: Sep 14, 2018, 02:04 pm by PieterP
Code: [Select]
ifstream input("input.csv");
AFAIK, the Teensy Core doesn't have a file system, so using ifstreams doesn't make any sense.
Even though the fstream classes seem to be in the STL header files, they don't have an implementation (or they just are not linked against your final executable), so you cannot use them.

On Arduino, you can use the entire C/C++ programming language, but you cannot use the entire STL.

The first error you're getting is because of the multiple definition of your CRGB leds[NUM_LEDS] array. You cannot define variables in the header file, because the header is used in multiple translation units (your main sketch and test.cpp, and possibly others if you expand your library). Declare it with external linkage in your header file, then define it once in the implementation file.

Code: (test.h) [Select]
#ifndef test_h
#define test_h

extern CRGB leds[NUM_LEDS];

#endif


Code: (test.cpp) [Select]
#include "test.h"

CRGB leds[NUM_LEDS];


Pieter

kschiang19

Thank you so much for your response! I found it extremely helpful.

Since the Teensy Core does not have a file system, would the alternative to a CSV file be transferring the data to arrays in an external cpp file?

westfw

Quote
would the alternative to a CSV file be transferring the data to arrays in an external cpp file?
Yes.

Go Up