arduino-libraries / ArduinoMqttClient

ArduinoMqttClient Library for Arduino
GNU Lesser General Public License v2.1
186 stars 73 forks source link

Linker error undefined reference to .. if I use the MQTT_CLIENT_STD_FUNCTION_CALLBACK #98

Closed ma-bg closed 6 months ago

ma-bg commented 6 months ago

I've added the example from here https://github.com/arduino-libraries/ArduinoMqttClient/pull/35#issue-597871517 to my code.

MqttClient::MessageCallback callback = [this](MqttClient *client, int messageSize) {
   Serial.print("Received message with topic: " + client->messageTopic());
};
mqttClient->onMessage(callback);

Then I got the following error:

undefined reference to MqttClient::onMessage(std::function<void (MqttClient*, int)>)

If I use the plain C function pointer things work fine. Any idea what might trigger my problem?

I'm using Arduino IDE 2.2.1 with an Arduino Nano ESP32.

per1234 commented 6 months ago

Hi @ma-bg. This feature is only available when the MQTT_CLIENT_STD_FUNCTION_CALLBACK macro is defined.

Unfortunately the comment in the source code regarding this is misleading:

https://github.com/arduino-libraries/ArduinoMqttClient/blob/f2574327f8e29c792225514e2d5fa00791a4e581/src/MqttClient.h#L35-L36

The comment implies that you can simply add the #define directive to the sketch:

MySketch.ino:

#define MQTT_CLIENT_STD_FUNCTION_CALLBACK
#include <ArduinoMqttClient.h>

// ...

But this is not possible. The reason is that this only defines the macro in the "translation unit" of the sketch, but not in the translation unit of MqttClient.cpp, where the macro must also be defined. If the macro is not defined in that translation unit, you get an undefined reference error.

There are two approaches to using the feature:

Arduino IDE 2.x does not provide an interface for the user to define arbitrary global macros so the former is only possible for Arduino IDE users by modifying the compilation command pattern in the boards platform. So the latter approach will be the best option for Arduino IDE users:

  1. Open the MqttClient.h file of your local installation of the library in any text editor.
  2. Change line 36 from this: https://github.com/arduino-libraries/ArduinoMqttClient/blob/f2574327f8e29c792225514e2d5fa00791a4e581/src/MqttClient.h#L36 To this:
    #define MQTT_CLIENT_STD_FUNCTION_CALLBACK
  3. Save the file.

If you would like further assistance with the configuration of the library or the usage of this feature, you are welcome to post over on Arduino Forum:

https://forum.arduino.cc/

I'm sure we'll be able to help you out over there.

ma-bg commented 6 months ago

Hi @per1234

I appreciate your detailed explanation & tecnical dive in. I changed my code and things work fine now.