Global Class Object declaration in multi code files project

Hi all.
This great forum have already helped me in various questions I had,
but now I cannot find a relevant topic
(or maybe I don't know the keywords for a decent search)

I am trying to use a class object as a library,
which is supplied by the manufacturer of a TFT Graphics controller IC (FTDI, FT800).
I am forced to use C++ as the library uses classes and templates and
it is not easy to me to port it in C.
The host microcontroller (atmega328) which controls the graphics controller (FT800) has only 32Kbytes flash,
so any byte is valuable.

In the header file FT_VM800P35.h there is a line defining a custom type
FT800IMPL_SPI which is used later for the declaration of the object
FTImpl.

FT_VM800P35.h :
1
2
3
4
5
6
7
8
9
10
#ifndef _FT_VM800P35_H_
#define _FT_VM800P35_H_
 ...
  #include "libraries/FT_GC/FT_Transport_SPI/FT_Transport_SPI.h"
 ... 
  #include "libraries/FT_GC/FT800/FT800Impl.h"
 ...
  typedef FT800Impl<FT_Transport_SPI> FT800IMPL_SPI;

#endif /* _FT_VM800P35_H_ */ 


FT800Impl.h, FT_Transport_SPI.h
are delivered by the manufacturer, as the above header file (FT_VM800P35.h).

Then, in my code file I have included the FT_VM800P35.h
and the line which declares the FTImpl using the FT800IMPL_SPI type.

1
2
3
4
...
#include <FT_VM800P35.h>
  FT800IMPL_SPI   FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
...


When the code is one file, bellow the above lines, everything compiles and
runs smoothly, example code showing the use of FTImpl :

HMI_someGraph.cpp:
1
2
3
4
5
6
7
8
9
 ...
  FTImpl.ColorRGB(120, 0, 120);
  FTImpl.LineWidth(16);
  FTImpl.Tag(HMI_Page_Header2);
  FTImpl.Begin(FT_RECTS);
  FTImpl.Vertex2f(261*16, 16);
  FTImpl.Vertex2f(320*16, 25*16);
  FTImpl.End();
 ...

But as the code is growing, I had to split the code to several files to
HMI_xxx.cpp and their HMI_xxx.h files.

The code in these files have to use the functions of FTImpl, but
I cannot find the way to do it.

I have tried making HMI_main.cpp and HMI_main.h files
with #include <FT_VM800P35.h> in the HMI_main.h file :

HMI_main.h :
1
2
3
4
5
6
#ifndef HMI_MAIN_H
#define HMI_MAIN_H
 ...
  #include <FT_VM800P35.h>
 ...
#endif //HMI_MAIN_H 

and
HMI_main.cpp :
1
2
3
4
  #include "HMI_main.h"
  ...
  FT800IMPL_SPI   FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
  ...

including #include "HMI_main.h" in the rest header files HMI_xxx.h
but with no success (" 'FTImpl' was not declared in this scope"),
for every HMI.xxx.cpp file.

and when I put
FT800IMPL_SPI FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
in the HMI_main.h file
HMI_main.h :
1
2
3
4
5
6
7
8
#ifndef HMI_MAIN_H
#define HMI_MAIN_H
 ...
  #include <FT_VM800P35.h>
 ...
  FT800IMPL_SPI FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
 ...
#endif //HMI_MAIN_H 

again no success ("multiple definition of FTImpl")
for every HMI.xxx.cpp file that uses its functions.

The only working attempt was when I declare FTImpl as static in every HMI_xxx.cpp file.
HMI_Config.cpp :
1
2
3
4
  #include "HMI_Config.h"
  ...
  static FT800IMPL_SPI   FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
  ...

and
HMI_Config.h :
1
2
3
4
5
6
#ifndef HMI_CONFIG_H
#define HMI_CONFIG_H
  ...
  #include <FT_VM800P35.h>
  ...
#endif // end HMI_CONFIG.H  

But this solution seems to me it consumes so much flash, as it seems the same code is repeated in every instance / declaration of every file uses it.

Sorry for my ignorance and the wrong terminology,
but as I said I am new in C++.
Although it is chance to improve my C++ programming skills I would appreciate any help.
Thanks.
Last edited on
Choose one header to declare FTImpl:
FTImpl.h
 
extern FT800IMPL_SPI   FTImpl;

Choose one source to define FTImpl:
FTImpl.cpp
 
FT800IMPL_SPI   FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);

Following this example, any source that needs to use FTImpl should include FTImpl.h. FTImpl.cpp should be linked in as any other source, or the linker will complain about undefined references.
Dear helios,
thanks for the quick reply.
I have just tried your idea, and it is compiling and running !!!
FTDI.h:
1
2
3
4
5
6
7
8
#ifndef _FTDI_H
#define _FTDI_H

#include <FT_VM800P35.h>

extern FT800IMPL_SPI  FTImpl;

#endif // end _FTDI_H 


and
FTDI.cpp :
1
2
#include "FTDI.h"
FT800IMPL_SPI FTImpl(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);


and
 
#include "FTDI.h" 

added in all the header files HMI_xxx.h their .cpp uses FTImpl.

Great feelings !!!

But the code size produced is almost the same as using
the static declarations (only few bytes difference in Flash and in RAM).

What is wrong? Shouldn't the code size be reduced ?
(I was hopping for an impressive reduction)
I don't know what to tell you. The method I describe creates a single static instance of the object. You could try allocating it dynamically. That would require only 4 bytes in flash.
I would give it a try but I do not know how I could allocate it dynamically.
Any ideas are welcomed.
1
2
3
4
5
extern FT800IMPL_SPI  *FTImpl;

//...

FT800IMPL_SPI *FTImpl = new FT800IMPL_SPI(FT_CS_PIN, FT_PDN_PIN, FT_INT_PIN);
Ok now you lost me. Definitely beyond my skills.
When you said about dynamic allocation I thought about the use of new and delete operators,
but as I had recently read I had the impression they are used for dynamic allocatiion in RAM of variables (like arrays).
After some google searching I could not found a way to use dynamic allocation of functions in the code.
Dear all, dearest helios,
it took me almost a day to merge all the files together,
to check what would be the minimum size of my code
using only once this library (class or whatever is called).

The method described by helios was the most easy (for me) to implement
and gave me the result described above (only few bytes gain).

After the merging (a lot of copy/paste - delete - formating etc.)
the result was again . . . only few bytes less
(and a file 2,760 lines code+comments).

So, there is no reason to bother anymore, the compiler did what it should do,
and by the method helios described I could use the structured program.

Now I have to work inside this library to get rid of what is not needed.

Thanks helios

Topic archived. No new replies allowed.