Build Error with ESP32: undefined reference

I’m getting the following error

 .pio\build\esp32dev\src\UnicViewAD.cpp.o:(.literal.lcm_write_bytes+0x0): undefined reference to `display'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32dev\firmware.elf] Error 1

I already tried moving the UnicViewAD.cpp to the /src folder but I’m still getting the same error.

Hope you can help me.

Out-of-tree source files are definitely wrong, a source file needs to be in either src/ or in a folder inside lib/ and referenced (e.g. via a header file) in code src/.

Which file creates the display variable?

I’m not really sure. This is the UnicViewAD.cpp file.


Okay. That just uses the extern keyword to tell the linker “somewhere in another .cpp this variable is created”, it’s a declaration / reference. Where’s the file that creates the variable?

If noone does, that’s a reference without a definition and you need to create aka define the variable.

And where do I define the variable?

Are you trying to copy over an old project from PlatformIO? If yes, it makes more sense to look for your missing file that creates this variable.

It doesn’t matter in which .cpp file the global variable is created. You could e.g. just exchange the extern LCM display; declaration for a definition which would be correct for your hardware / application. It seems to at least want a Serial-type object in its constructor per LCM | UnicView AD.ino - Biblioteca para Arduino.

However, it more seems like to me that the library file should never be modified, and instead in your user application (src/main.cpp) etc you should call the created LCM display object display.

So I removed the extern LCM display in the UnicViewAD.cpp file and it worked!
I’m not sure if I was supposed to do this, but the code compiled.