Using hd44780 extensible library with multiple files

I am trying to use the hd44780 extensible library in several sepparate code files. GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library
I have prepared a skeleton version of my code that still reproduces the same error.
my .ino file:

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

#include "secondPage.h"

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

void setup() {
    Wire.begin();
    lcd.begin(16,2);
    badFunc();
}

void loop() {
    
}

secondPage.h

#ifndef SECOND_PAGE_H
#define SECOND_PAGE_H

extern hd44780_I2Cexp lcd;      // i thought this might help declare the lcd object
                                // error: 'hd44780_I2Cexp' does not name a type

void badFunc(void);

#endif

secondPage.cpp

#include "secondPage.h"

extern hd44780_I2Cexp lcd; 

void badFunc(void) {
    // error: 'lcd' was not declared in this scope
    lcd.print("Hello World");
}

I've seen many posts with the exact problem occuring when using the LiquidCrystal library. But there is no information on how to solve this specifically for the hd44780 library. The LiquidCrystal library actually takes input parameters when instantiating the lcd object. So there is a clear difference between declaring (don't give input parameters) and defining (give all the pin numbers connected to the LCD). Please correct me if my understanding is wrong, but I don't see how to differentiate between the two in the hd44780 library. I thought that declarations should be put in .h files and definitions in .cpp files, but have no idea what to do when it comes to the hd44780 library.

I suspect that you have more errors. This is what I get

In file included from /Home/wim/Downloads/1_arduino_ide_1.x/arduino-1.8.19/portable/sketchbook/forum.arduino.cc/1278528/1278528.post01/secondPage.cpp:1:0:
secondPage.h:4:8: error: 'hd44780_I2Cexp' does not name a type
 extern hd44780_I2Cexp lcd;      // i thought this might help declare the lcd object
        ^~~~~~~~~~~~~~
secondPage.cpp:3:8: error: 'hd44780_I2Cexp' does not name a type
 extern hd44780_I2Cexp lcd;
        ^~~~~~~~~~~~~~
/Home/wim/Downloads/1_arduino_ide_1.x/arduino-1.8.19/portable/sketchbook/forum.arduino.cc/1278528/1278528.post01/secondPage.cpp: In function 'void badFunc()':
secondPage.cpp:7:5: error: 'lcd' was not declared in this scope
     lcd.print("Hello World");
     ^~~

You will need to include the needed libraries in your cpp file.

secondPage.cpp

#include "secondPage.h"

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header


extern hd44780_I2Cexp lcd; 

void badFunc(void) {
    // error: 'lcd' was not declared in this scope
    lcd.print("Hello World");
}

secondPage.h is only there to tell a file that needs to call badFunc() (in your case the main file (ino file)) what the prototype of badFunc() is. There is no need (in your example), to include it in secondPage.cpp; I did leave it in but you can remove that include.

secondPage.h also does not need the extern declaration. But fi you keep it, you again need to include the needed library.

secondPage.h

#ifndef SECOND_PAGE_H
#define SECOND_PAGE_H

void badFunc(void);

#endif

Thankyou!! That fixed my problem. And yes I did get the same errors as you, just didn't type them out accidentally.

You don't have to type them out; it a matter of copy/paste :smiley: It's preferred to use code tags for that so it's easier to copy when the output is long.

You can mark your topic as solved by clicking the little check button under the most useful post so others know that a solution was provided.