knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

New Compile time warnings with PubSubClient 2.7.0 in Arduino IDE 1.8.7 #537

Open WombatHollow opened 5 years ago

WombatHollow commented 5 years ago

Just returned to my project after 6 months, and updated PubSubClient to 2.7.0 and now get 5 warnings in PubSubClient package at compile time. Went back to 2.4.0 and these warnings disappear.

C:\Users\ianh4\Documents\Arduino\Sketchbooks\libraries\PubSubClient\src/PubSubClient.h:88:7: warning: type 'struct PubSubClient' violates one definition rule [-Wodr]

 class PubSubClient : public Print {
       ^

C:\Users\ianh4\Documents\Arduino\Sketchbooks\libraries\PubSubClient\src\PubSubClient.h:88:7: note: a different type is defined in another translation unit

 class PubSubClient : public Print {
       ^

C:\Users\ianh4\Documents\Arduino\Sketchbooks\libraries\PubSubClient\src/PubSubClient.h:91:39: note: the first difference of corresponding definitions is field 'buffer'

    uint8_t buffer[MQTT_MAX_PACKET_SIZE];
                                       ^

C:\Users\ianh4\Documents\Arduino\Sketchbooks\libraries\PubSubClient\src\PubSubClient.h:91:39: note: a field of same name but different type is defined in another translation unit

    uint8_t buffer[MQTT_MAX_PACKET_SIZE];
                                       ^

lto1.exe: note: array types have different bounds

In regard to MQTT_MAX_PACKET_SIZE, I do define that as 256 (this produces a warning in 2.4.0 (fixed in 2..7.0) but that I can fix) otherwise it is all a standard code as below (just a snippet)

#include <SPI.h>
#include <Ethernet2.h>
#define SS     10U    //D10 ---- SS 
#define RST    11U    //D11 -----Reset

//MQTT Client Libraries
#define MQTT_MAX_PACKET_SIZE 256   
#include <PubSubClient.h>

Target Environment is Leonardo with W5500 Ethernet.

I haven't got to loading and running again to see if this runs OK, I have a few things to build and more Code to write.

Any idea why I get these warnings?

knifter commented 5 years ago

You'll have to define MQTT_MAX_PACKET_SIZE in the makefile or on the commandline. Maybe Arduino IDE has some means to make defines, I don't know. I personally don't use the IDE.

What's happening now is that while compiling your main.cpp (wrapping your Arduino sketch .uno) MQTT_MAX_PACKET_SIZE gets defined as 256 which propagates to the interpretation of PubSubClient.h. Before that the compile process has compiled PubSubClient.cpp (which is also including PubSubClient.h) with the MQTT_MAX_PACKET_SIZE = default. This means the the PubSubClient.o contains a buffer['default'] while your main.o refers to a buffer[256]. And that is what the (I guess) linker is complaining about.

WombatHollow commented 5 years ago

Commented out the #define and warnings went away. Found I could #define MQTT_MAX_PACKET_SIZE 128 but set it to any value smaller or larger and you get the warnings?

Maybe an issue to fix next time as the .h file has a #ifndef MQTT_MAX_PACKET_SIZE switch on it which I thought would stop it overwriting the buffer size set in the sketch.

Thanks for your help