Spreading out accross multiple files

First, I would like to say I love PlatformIO! As a newer user I am expanding my programming skills and have run into a problem I was not able to find an answer to either on this forum or googling. Perhaps I am not using the correct terminology?

I started to experiment but realized I don’t want to hack at it rather I would like to know the proper way to do this. So what am I talking about?

Well, my project is getting too big for one file and I would like to have multiple files. So, my main.cpp uses the #include <TFT_eSPI.h> where I invoke the following library:
TFT_eSPI tft = TFT_eSPI();

And the void setup(){} makes calls to the library, for example:
tft.setTextColor(TFT_WHITE,TFT_BLACK);
And it all works well.

Now I need to create second file so I create the file called graph.cpp with graph.h where I still want to make calls to this invoked library. But the compiler does not understand what the tft part of tft.setTextColor(TFT_WHITE,TFT_BLACK); int the second file and I do not know how to fix this?

Thank you.

Defines a variable, tft, in the file main.cpp. This file with any headers, is a single “compilation unit”.

No variables in one compilation unit are visible in other compilation units. Graph.cpp is another compilation unit, so cannot see tft.

However, if you tell the other units that tft is extern, then it will now be visible. So, add this to graph.cpp:

extern TFT_eSPI tft;

That should fix it.

Cheers,
Norm.

Thank you for your reply Norm. I tried it and I get the following error:
src\graph.cpp:8:8: error: ‘TFT_eSPI’ does not name a type
extern TFT_eSPI tft; //= TFT_eSPI(); // Invoke library, pins defined in User_Setup.h

I have googled this error and I still do not understand it.

Oh, and sorry I have no idea how to insert code here as I don’t’ see any button for that. I used “preformated text” but until I post it I don’t know if it will work.

#include <TFT_eSPI.h> before your extern declaration. Otherwise the type willl not be known.

1 Like

Use three or more back ticks to insert code:

```
Some code
    Goes here.
```

@maxgerhardt has already mentioned how to get the data type to be known for tft.

Cheers,
Norm.

Yes, Norm, thank you. I cannot believe I missed that. :frowning:

Oh, and thank you for posting code. I have saved that in my wiki for next time.

Cheers,
Steve

1 Like