Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ma-bg opened this issue Jan 31, 2024 · 2 comments
Assignees
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@ma-bg
Copy link

ma-bg commented Jan 31, 2024

I've added the example from here #35 (comment) 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 per1234 self-assigned this Jan 31, 2024
@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Jan 31, 2024
@per1234
Copy link
Contributor

per1234 commented Jan 31, 2024

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:

// Make this definition in your application code to use std::functions for onMessage callbacks instead of C-pointers:
// #define MQTT_CLIENT_STD_FUNCTION_CALLBACK

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:

  • Define the macro globally via a -D flag in the compilation command.
  • Define the macro in the library header file.

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:
    // #define MQTT_CLIENT_STD_FUNCTION_CALLBACK

    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.

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2024
@ma-bg
Copy link
Author

ma-bg commented Feb 1, 2024

Hi @per1234

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants