First try with .h and .cpp, Multiple Definition issue

Hi guys,
I'm not very expert with arduino and with programming in general;

I make a CPS (Concented Solar Power) for my thesis ( i'm becoming a mechanical engineer) and i'm trying to develope an automated system for the Solar Traking and for putting temperature data online on a chart.

Every thing works pretty well, but I'd love to split the code in come .h and .cpp files to make it more "professional".

So I splitted the loop in a few functions(FS,SC,PrintTime), but i'm getting this issue:


sketch\FS.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\PrintTime.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\SC.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

collect2.exe: error: ld returned 1 exit status


I Think the problem in related to: all the function print something on the display (SSD1306). I created a .h file with all the delarations for that display, then i included it in every .cpp files.
I have tried lot of thing, i cannot understand why it says there are multiple definition.

Could someone help me please?
Thanks in advance guys,
RIccardo.

"Concented"

Concentric?
Concentrated?

Error code, but no code == no help

1 Like

Without seeing your complete code it is difficult to provide any help so please post your sketch and library files

Please post your code following the advice given in the link below

1 Like

please post your code using </>

1 Like

Read the error messages, then be sure you are repeating the declarations, not the definitions of all those things it’s complaining about.

a7

Are you maybe missing the "extern" keyword?

Thank guys,
I miss a few things like the code to put data on web( already tested alone)
And 2 temperature sensor.

l'll try to share the code..

Main one


#include "PINOUT.h"
#include "FS.h"
#include "SC.h"
#include "PrintTime.h"

boolean FS = true ; 

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;


//////time/////////
const char* ssid       = "TP-LINK_A7650E";
const char* password   = "*************";

//////////////////////////

void setup() {
  Serial.begin(115200);
    
  pinMode ( LS1, INPUT );
  pinMode ( LS2, INPUT );
  pinMode ( LS3, INPUT );
  pinMode ( LS4, INPUT );
  
  pinMode ( MTC, OUTPUT );
  pinMode ( MTA, OUTPUT );
  pinMode ( MPA, OUTPUT );
  pinMode ( MPO, OUTPUT );
  
  pinMode ( SMAXTILT, INPUT );
  pinMode ( SMINTILT, INPUT );
  pinMode ( SMAXPAN , INPUT );
  pinMode ( SMINPAN, INPUT );
  
  digitalWrite ( MTC, HIGH);
  digitalWrite ( MTA, HIGH);
  digitalWrite ( MPA, HIGH);
  digitalWrite ( MPO, HIGH);

/////OLED_I2C///////

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  
printLocalTime();

}


void loop() {

 if ( FS == true){FirstStart(); FS = false; } ////FIRST START
 
  SolarCheck();

}



FS.ccp (first start = open to max)

#include "FS.h"


void FirstStart(){
  Serial.println ("1st start");
  Serial.println ("tilt");
  
  display.clearDisplay();
  display.setCursor(0, 0); 
  display.println("First Start Adj..");
  display.display();
  
  display.print("Tilt Axis..");
  display.display();
       
    while (digitalRead(SMAXTILT) == 1){ digitalWrite(MTA, LOW);} 
    digitalWrite(MTA, HIGH);
         Serial.println ("tilt max");
        
         display.println("Tilt Max!");
         display.println("Pan Axis...");
         display.display();
         
         Serial.println ("pan");
         
    while (digitalRead(SMAXPAN) == 1){digitalWrite(MPA, LOW);}
    digitalWrite(MPA, HIGH);
        Serial.println ("pan max");
    
        display.print("Pan Max!");
        display.display();
        display.clearDisplay();

        delay(2000);
} 

FS.h


#ifndef FS_H
#define FS_H


#include "PINOUT.h"
#include "displaydef.h"

void FirstStart();

#endif

SC.cpp (Solar Check)

/////////////////////////////////////
/////////SOLAR CHECK////////////////
////////////////////////////////////

#include "SC.h"
#include "FS.h"

unsigned long t1, dt; // X SOLARCHECK

void SolarCheck(){


  int LS1Val = analogRead(LS1);
  int LS2Val = analogRead(LS2);
  int LS3Val = analogRead(LS3);
  int LS4Val = analogRead(LS4);

  
  dt= millis() - t1;
  if(dt >= 5000)
  
  {
    
  t1=millis();
  
  Serial.println("Solar Check");
  display.clearDisplay();
  display.setCursor(0,0);
  display.println("Solar Check");
  display.println("");
  
  Serial.print("SX: ");
  Serial.println(LS1Val); 
  display.setCursor(0,16);
  display.print("Sx: ");
  display.println(LS1Val);
  if ( LS1Val > 600 & LS1Val > (LS2Val*1.2) ) {digitalWrite ( MPO , LOW); delay(500);digitalWrite ( MPO , HIGH);} 
  else {digitalWrite ( MPO , HIGH);}


  Serial.print("DX:  ");
  Serial.println(LS2Val); 
  display.print("Dx: ");
  display.println(LS2Val);
  if ( LS2Val > 600 & LS2Val > (LS1Val*1.2) ) {digitalWrite ( MPA , LOW);delay(500);digitalWrite ( MPA , HIGH);} 
  else {digitalWrite ( MPA , HIGH);}

Serial.print("UP:  ");
  Serial.println(LS3Val); 
  display.print("UP: ");
  display.println(LS3Val);
  if ( LS3Val > 600 & LS3Val > (LS4Val*1.2) ) {digitalWrite ( MTA , LOW); delay(500);digitalWrite ( MTA , HIGH);} 
  else {digitalWrite ( MTA , HIGH);}

  Serial.print("DOWN:  ");
  Serial.println(LS4Val); 
  display.print("Down: ");
  display.println(LS4Val);
  if ( LS4Val > 600 & LS4Val > (LS3Val*1.2) ) {digitalWrite ( MTC , LOW); delay(500);digitalWrite ( MTC , HIGH);} 
  else {digitalWrite ( MTC , HIGH);}
  display.display();
  display.clearDisplay();

  }
  
if (digitalRead(SMINPAN) == 0 & LS1Val > 200){
  Serial.print ("RO Pan..");
  display.setCursor(0,0);
  display.print("RO Pan..");
  display.display();
  while(digitalRead(SMAXPAN) == 1){digitalWrite(MPA, LOW);}
  Serial.print ("Max Pan!");
  display.println("Max Pan");
  display.display();
  display.clearDisplay();} ///REOPEN


if (digitalRead(SMINTILT) == 0 & LS1Val > 200){
  Serial.print ("RO Tilt..");
  display.setCursor(0,0);
  display.print("RO Tilt..");
  display.display(); 
  while(digitalRead(SMAXTILT) == 1){digitalWrite(MTA, LOW);}
  Serial.print ("Max Tilt!");
  display.println("Max Tilt");
  display.display();
  display.clearDisplay();}  ///REOPEN


  delay(100);
}

SC.h

#ifndef SC_H
#define SC_H

#include "PINOUT.h"
void SolarCheck();

#endif

displaydef.h ( i made this to just define all i need for display print one time)

#ifndef displaydef_H
#define displaydef_H

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);




#endif

PINOUT.h (i don't now if it's good or it's better to keep it in main)

#ifndef PINOUT_h
#define PUNOUT_H

///////input///////

  ///luxmeters//
   #define LS1 33 //sx
   #define LS2 32 //dx
   #define LS3 34 //up
   #define LS4 35 //down

  ///microswitch
  #define SMAXTILT 15     
  #define SMINTILT 2
  #define SMAXPAN 4
  #define SMINPAN 0


////output//////

  ///MOTORS
  #define MTC 18 //Tilt Chiudere
  #define MTA 5 //Tilt Aprire
  #define MPA 16 //Pan AntiORario
  #define MPO 17  //Pan Orario

  #endif

Print time.h ( printlocaltime() is just an example copyed from esp time library)

#ifndef PrintTime_H
#define PrintTime_H

#include "time.h"
#include <WiFi.h>
//#include "displaydef.h"


void printLocalTime();

#endif

Concentrated..
Sorry wrote it so fast..

Yep sorry, it was Definition not declaration

sketch\FS.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\PrintTime.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\SC.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Have I to write it? where?

the above in displaydef.h causes display to be defined in every .ino/.cpp file displaydef.h is included in

i think display can be defined as an extern in the .h

extern Adafruit_SSD1306 display;

See My Reply #5 Here.

1 Like

So like this..

Still not working

#ifndef displaydef_H
#define displaydef_H

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C
extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);


#endif

Error Code

sketch\FS.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\PrintTime.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

sketch\SC.cpp.o:(.bss.display+0x0): multiple definition of `display'

sketch\Controller.ino.cpp.o:(.bss.display+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Error compiling for board ESP32 Dev Module.

no

like this

OK so what I did it's basically good..I'll try to put all the include on the cpp...

Without " (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); " it's not working, it says :
sketch\Controller.ino.cpp.o:(.literal._Z5setupv+0x10): undefined reference to display'`

don't know what this means

the above, the definition/constructor is needed in the .cpp file

the above declaration is needed in the .h file so that the compiler knows about display in .ino/.cpp files referencing it.

1 Like

Thanks for your patience..
I tried and i got this

SC.cpp:7:8: error: 'Adafruit_SSD1306' does not name a type

 extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

I think I'll re-put everything together and f**k the "professional Look" :joy:

First of all thanks for the answere you linked, I saved it!
Then, i tried, but if I put the include I get a thousands error becouse it doesn't recognize the display. commands ( like display.display() display.print etc)

There are proponents of this approach.

Just make the soup, everything in one pot, delicious and if you need the carrot, fish around in there and find it.

Srsly, until you understand what you did, what you did wrong and why what doing as has been suggested here worked, to the extent that it did, it makes no sense to just go about tossing extern at things and otherwise fiddling with your code.

a7