0

This is, hopefully, a simple issue!

I'm trying to access my fully working libraries (fully compiles, and works with the hardware perfectly) from another library (Computer.h) to abstract it away from the Sketch.ino file.

LLRelay, and all my other libraries, work flawlessly when instanced/constructed/built/etc within the main Sketch.ino; however, when called within "Computer.h", there's issues.

I've been at this for most of the day trying different things, from instantiating the libraries in the header files, the *.cpp files, to within functions, to changing the scope and publicity/privacy of them - still no luck. See the (2 * "HERE -") comments for the errors.

Any help is greatly appreciated!

This is what I have managed to reduce it down to for debugging:

Sketch.ino

// Sketch.ino
#include "Computer.h"

Computer pc1;

void setup(){};

void loop(){ 
    pc1.update(); 
};

Computer.h

// "../customLibs/Computer.h"
#ifndef Computer_lib
#define Computer_lib
#if(ARDUINO > 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif

class Computer {
    public:
        Computer();
        void update();
    private:
        void fans();
};
#endif

Computer.cpp

// "../customLibs/Computer.cpp"
#include "Computer.h"
#include "src/relay/LLRelay.h" /* Potential culprit, I have tried many variations and locations for this to go, all with the same, or similar compilation errors */

/* HERE - "undefined reference to `LLRelay::LLRelay(unsigned char)'" */
LLRelay fan1(13); // fan1(uint8_t relayPin);

Computer::Computer(){
    Serial.begin(9600);
};

void Computer::update(){
    fans();
};

void Computer::fans(){

    /* HERE - "undefined reference to `LLRelay::On()'" */
    fan1.On();

};

LLRelay.h

// "../customLibs/src/relay/LLRelay.h"
#ifndef LLRelay_lib
#define LLRelay_lib
#if(ARDUINO > 100)
  #include "Arduino.h"
#else
    #include "WProgram.h"
#endif

class LLRelay {
  public:
    LLRelay(uint8_t relayPin);
    void On();
    void Off();
  private:
    uint8_t _relayPin;
};
#endif

LLRelay.cpp

// "../customLibs/src/relay/LLRelay.cpp"
#include "LLRelay.h"

LLRelay::LLRelay(uint8_t relayPin){
  _relayPin = relayPin;
  pinMode(_relayPin, OUTPUT);
  digitalWrite(_relayPin, LOW);
};

void LLRelay::On(){
  digitalWrite(_relayPin, HIGH);
};

void LLRelay::Off(){
  digitalWrite(_relayPin, LOW);
};
1

The Arduino IDE must detect the library to include it in the build process. It is done by matching a library to a header file by searching the libraries in configured library locations. Only the root folder and the src subfolder of a library are searched.

If your library is in the sketch folder's libraries subfolder and it is called customLibs, then only the customLibs folder and the customLibs/src subfolder are searched and the include file LLRelay.h in customLibs/src/relay folder is not found.

reference: Layout of library folders and files

0

The Arduino build process does not consider libraries used from other libraries. You will need to include src/relay/LLRelay.h in the main project file in order to make it compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.