#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.