Custom Header not found + File Structure Question

Hi Everyone, I’m a bit new when it comes to the ESP32 platform and C++ in general. I’ve studied computer science in high school but It only took me as far as interfaces and data structures in C#, So I get the general gist of things but I’m a bit confused when It comes down to the syntax and Implementation of things.

So my goal was to draw a simple menu using C++ on a TFT LCD using the Adafruit GFX Library and the Adafruit ST7789 library. I created a header file and a C++ Implementation file for a simple button design with some text in it for ease of use and so that the project code wouldn’t look like a rat’s nest of code. The problem Is that I’ve modified the code in the Adafruit_GFX library and whenever I try to include the header file in those libraries VSCODE doesn’t see the header file during compliation (but Intellisense is working just fine) so I’m not even sure I’m doing any of this right, and so I suspect it’s got something to do with my project’s file structure. I’ve attached a picture of my file structure and the code, would love some help!

And the way I include the header file is I just write “#include <button.h>”

“button.h”

#pragma once

class BUTTON {
private:
  /* data */
public:
  uint8_t _index;
  boolean _selected;
  int16_t _rect_x0;
  int16_t _rect_y0;
  int16_t _rect_w;
  int16_t _rect_h;
  int16_t _rect_radius;
  const char *_string;
  uint8_t _stringSize;
  boolean _textWrap;
  BUTTON(uint8_t index, boolean selected, int16_t rect_x0, int16_t rect_y0,
         int16_t rect_w, int16_t rect_h, int16_t rect_radius,
         const char *string, uint8_t stringSize, boolean textWrap);
  ~BUTTON();
};

“button.cpp”

#include "button.h"
#include <Arduino.h>

BUTTON::BUTTON(uint8_t index, boolean selected, int16_t rect_x0,
                    int16_t rect_y0, int16_t rect_w, int16_t rect_h,
                    int16_t rect_radius, const char *string, uint8_t stringSize,
                    boolean textWrap) {
  _index = index;
  _selected = selected;
  _rect_x0 = rect_x0;
  _rect_y0 = rect_y0;
  _rect_w = rect_w;
  _rect_h = rect_h;
  _rect_radius = rect_radius;
  _string = string;
  _stringSize = stringSize;
  _textWrap = textWrap;
}

What’s this header file doing with referencing types from stdint.h without #include <stdint.h>?

Completely wrong file structure, as lib/README says, the exepcted structure in lib is that you create a new folder in which the header and source files are placed. Please carefully read through that file and restructure to lib/Button/button.{cpp, h}.

Having to modify Adafruit_GFX seems to be very weird, what functionality did it lack or what bug did it have that required modification?

Also, if a library (with the file structure as defined above) cannot include its sub-dependencies correctly (like Button may e.g. need Adafruit_GFX), the first thing to look at is lib_ldf_mode.

1 Like

Actually, if you’re trying to #include <button.h> in the Adafruit_GFX library, then yes with your current file structure that will go very wrong. If you restructure button to be a library as described above, it will work normally.

Thanks for the help, I’ve restructured the project files and it looks like this, hope that I understood what you meant:

And I fixed the header file that was missing some references, but now I’m getting a completely different error:

collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32doit-devkit-v1\firmware.elf] Error 1

And in regards to the Adafruit library… It’s not that It’s buggy or missing some feature, It’s just that I felt like it would be more appropriate if and when I wanted to create a custom button with some text that the “static” calculations would be done in a sort of library, and if I place the function inside the Adafruit library then I wouldn’t need to keep referencing the ST77889 object every time.

The lines before that are crucical. Those tell you what function implementation or variable wasn’t found, or what went wrong in the linking. This error just says “woopsie, couldn’t link :)”

Yeah sorry about that. Still got some learning to do, here’s the full error:

c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32doit-devkit-v1\src\main.cpp.o:(.literal._Z5setupv+0x1c): undefined reference to BUTTON::~BUTTON()' c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32doit-devkit-v1\src\main.cpp.o: in function setup()‘:
C:\Users\Admin\Documents\PlatformIO\Projects\ESP32_TEST/src/main.cpp:52: undefined reference to BUTTON::~BUTTON()' c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\Admin\Documents\PlatformIO\Projects\ESP32_TEST/src/main.cpp:53: undefined reference to BUTTON::~BUTTON()’
c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: C:\Users\Admin\Documents\PlatformIO\Projects\ESP32_TEST/src/main.cpp:50: undefined reference to BUTTON::~BUTTON()' c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\Admin\Documents\PlatformIO\Projects\ESP32_TEST/src/main.cpp:49: undefined reference to BUTTON::~BUTTON()’
c:/users/admin/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32doit-devkit-v1\src\main.cpp.o:C:\Users\Admin\Documents\PlatformIO\Projects\ESP32_TEST/src/main.cpp:53: more undefined references to `BUTTON::~BUTTON()’ follow
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32doit-devkit-v1\firmware.elf] Error 1

Okay. So what is the declaration of destroctor of the BUTTON class the header in the button.h? And what’s its implementation in button.cpp? The compiler says that one was declared but never implemented.

1 Like

First Off I’d like to thank you because It actually works now! The main problem was the file structure so I’m going to mark your original answer as the actual answer, but the problem that the compiler was throwing at me was caused by the fact that I didn’t implement

~BUTTON()

But I did declare it, funny thing Is it was auto-generated by VSCODE so I had forgotten It was even there.

Thanks Again!