multiple definition

Hello everybody,

I have an application (séquenceur.ino) that uses UTFT and UTouch libraries.

For this application, I have written a library (ChPComp) that is also using UTFT and UTouch. So I have declared in the file ChPComp.h the variables myGLCD and myTouch :

UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch myTouch(6, 5, 4, 3, 2);

This is the only place where these variables are declared. They are used both in sequence.ino and ChPComp.cpp.

When I compile the application, I have the following error :

ChPComp\ChPComp.cpp.o:(.bss.myGLCD+0x0): multiple definition of `myGLCD'
Sequenceur.cpp.o:(.bss.myGLCD+0x0): first defined here
c:/documents and settings/pierre/mes documents/mes téléchargements/arduino-1.5.2/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
ChPComp\ChPComp.cpp.o:(.bss.myTouch+0x0): multiple definition of `myTouch'
Sequenceur.cpp.o:(.bss.myTouch+0x0): first defined here

I dont know how to exploit this information.

So, how to solve this problem ?

Thank you in advance for your help.

Pierre

So I have declared in the file ChPComp.h

This is the only place where these variables are declared.

But probably included twice?
There's your answer.

So I have declared in the file ChPComp.h the variables myGLCD and myTouch :

The question, of course, is where in that file the variables are defined. And, also, whether the file includes proper include guards to prevent multiple inclusion.

So, how to solve this problem ?

Write the code correctly. If you need help with that, you need to post the code.

Thank you for your replies,

here is the begining of code for each file :

Sequenceur.ino :

#include <ChPComp.h>
#include <Constantes.h>
#include <tinyFAT.h>
#include <UTouch.h>
#include <UTFT.h>
#include <DS1307.h>
...

Constantes.h :

#ifndef Constantes_h
#define Constantes_h

#define NB_ZONES  6
#define NB_CYCLES  4

#endif

ChPComp.h :

#ifndef ChPComp_h
#define ChPComp_h
#include <Constantes.h>

#include <arduino.h>
#include <UTFT.h>
#include <UTouch.h>

UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch myTouch(6, 5, 4, 3, 2);
...

and finally ChPComp.cpp :

#include <Arduino.h>
#include <UTFT.h>
#include <UTouch>
#include <ChpComp.h>

extern uint8_t TinyFont[];
...

Thank you for your help.

Pierre

  1. You only define variables in .c or .cpp files, never in .h files.
  2. You declare external references to the variables in .h files.

I.e.,

UTFT myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch myTouch(6, 5, 4, 3, 2);

should be in a .cpp file, and

extern UTFT myGLCD;
extern UTouch myTouch;

should be in the .h file.

Thank you "majenko". I applied what you suggested and now it works fine.

I have a lot to learn about C++

Sincerely.

Pierre

here is the begining of code for each file :

I was going to say something like "Here is the start of the answer...", but majenko already provided the answer, so I'll resist the temptation to post a smart ass answer.

See reply #1