0

I'm developing an IoT C++ application and I want to send the data over MQTT. Therefore, I created a header-class for my special needs:

#include <mqtt/client.h>

class ClientMQTT{
    public:
        ClientMQTT();
        ClientMQTT(std::string _mqtt_ip_address, int _mqtt_port, std::string _mqtt_username, std::string _mqtt_password, std::string _client_id, bool no_publish);
        int mqtt_send(char *message, int msg_length, std::string topic);
        void report_error(std::string timestamp, std::string message);

        bool connect();
        bool disconnect();

        mqtt::client *client;
        std::string mqtt_ip_address;
        int mqtt_port;
        std::string mqtt_username;
        std::string mqtt_password;
        std::string client_id;
        bool no_publish;
};

For (dis)connecting, i created two functions that I can call when the program starts and exits, bool connect() and bool disconnect(). The body looks like this:

bool ClientMQTT::disconnect(){
    if(this->client == NULL) return true;

    this->client->disconnect();

    return !(this->client->is_connected());  //this makes problems!
}

When disconnecting, I need to check whether the MQTT client really has disconnected or not. So i call the virtual bool mqtt::client::is_connected() function.

The problem is, that I get the following error when compiling/linking: /usr/bin/ld: obj/Debug/CAN_Reader.o||undefined reference to symbol 'MQTTAsync_isConnected'

But I linked the MQTT-Library with -lpaho-mqttpp3, cause without this code line, the program compiles and works ...

Does anybody of you know why this error occurs?

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Michael Gierer
  • 401
  • 2
  • 6
  • 15
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Jesper Juhl Feb 25 '18 at 17:39

1 Answers1

0

I had the same error trying to use, I think, the same MQTT Paho C++ code. But I am using VS 2015 on Windows, so I think this is different from you? For me the problem was in MQTTAsync.h. On line 95 the code is detecting whether or not it is a Windows environment. In this case, it missed it and thought I was on linux. This didn't work:

#if defined(WIN32) || defined(WIN64)

I made it be

#if defined(WIN32) || defined(WIN64) || defined(_WIN32)

And that last one seems to be a good define. This affects whether it uses "declspec" (Windows) or "attribute" (linux) to access the library. After that I'm guessing it could read my Windows library better. So I am down from 170 errors to only 36! Progress... Again, I'm not sure it is the same problem, but maybe it will give you a hint.

Andrew
  • 81
  • 1
  • 3
  • Err. scratch that. The missing flag was part of the problem, but there is more going on here. I will keep you posted. It almost seems like they are abstract functions, or callbacks. Writing a definition for it fixes the problem. I'll keep digging. – Andrew Mar 01 '18 at 13:08
  • In the end, to remove all link errors, I needed to have both paho-mqtt3a.lib and paho-mqttpp3.lib readily available. Originally I was trying to use paho-mqtt3c.lib, or even paho-mqtt3-static.lib, and neither of those version c libs worked. So to repeat, both the correct underlying c lib, and the correct cpp lib need to be available, plus the headers for both c and cpp libraries. Now I'll see if this compiled code actually does anything! – Andrew Mar 01 '18 at 14:26