首页 > 解决方案 > 使用 MQTT 和 C++ 无法解析的外部符号?

问题描述

我正在尝试在我的笔记本电脑上初始化客户端服务器以使用 MQTT 连接到我的树莓派,我正在使用 C++ 来执行此操作,这是我正在尝试执行的代码,这是 mqtt.cpp:

#include "mqtt.h"
#define PUBLISH_TOPIC "EXAMPLE_TOPIC"

#ifdef DEBUG
#include <iostream>
#endif

mqtt_client::mqtt_client(const char *id, const char *host, int port) : mosquittopp(id)
{
    int keepalive = DEFAULT_KEEP_ALIVE;
    connect(host, port, keepalive);
}

mqtt_client::~mqtt_client()
{
}

void mqtt_client::on_connect(int rc)
{
    if (!rc)
    {
        #ifdef DEBUG
            std::cout << "Connected - code " << rc << std::endl;
        #endif
    }
}

void mqtt_client::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
    #ifdef DEBUG
        std::cout << "Subscription succeeded." << std::endl;
    #endif
}

void mqtt_client::on_message(const struct mosquitto_message *message)
{
    int payload_size = MAX_PAYLOAD + 1;
    char buf[payload_size];

    if(!strcmp(message->topic, PUBLISH_TOPIC))
    {
        memset(buf, 0, payload_size * sizeof(char));

        /* Copy N-1 bytes to ensure always 0 terminated. */
        memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char));

        #ifdef DEBUG
            std::cout << buf << std::endl;
        #endif

        // Examples of messages for M2M communications...
        if (!strcmp(buf, "STATUS"))
        {
            snprintf(buf, payload_size, "This is a Status Message...");
            publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
            #ifdef DEBUG
                std::cout << "Status Request Recieved." << std::endl;
            #endif
        }

        if (!strcmp(buf, "ON"))
        {
            snprintf(buf, payload_size, "Turning on...");
            publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
            #ifdef DEBUG
                std::cout << "Request to turn on." << std::endl;
            #endif
        }

        if (!strcmp(buf, "OFF"))
        {
            snprintf(buf, payload_size, "Turning off...");
            publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
            #ifdef DEBUG
                std::cout << "Request to turn off." << std:: endl;
            #endif
        }
    }
}

这是头文件 mqtt.h:

#ifndef SIMPLECLIENT_MQTT_H
#define SIMPLECLIENT_MQTT_H

#include "mosquittopp.h"
#include <cstring>
#include <cstdio>

#define MAX_PAYLOAD 50
#define DEFAULT_KEEP_ALIVE 60

class mqtt_client : public mosqpp::mosquittopp
{
public:
    mqtt_client (const char *id, const char *host, int port);
    ~mqtt_client();

    void on_connect(int rc);
    void on_message(const struct mosquitto_message *message);
    void on_subscribe(int mid, int qos_count, const int *granted_qos);
};

#endif //SIMPLECLIENT_MQTT_H

现在,我主要编写以下代码:

class mqtt_client *iot_client;
int rc;

char client_id[] = CLIENT_ID;
char host[] = BROKER_ADDRESS;
int port = MQTT_PORT;

mosqpp::lib_init();
if (argc > 1)
    strcpy_s(host, argv[1]);

iot_client = new mqtt_client(client_id, host, port);

while (1)
{
    rc = iot_client->loop();
    if (rc)
    {
        iot_client->reconnect();
    }
    else
        iot_client->subscribe(NULL, MQTT_TOPIC);
}

mosqpp::lib_cleanup();

构建和编译后,我收到 6 个“未解决的外部符号”错误,我不知道为什么,我已经尝试了 3 天,但没有运气。我希望有 MQTT 经验的人能帮助我。

编辑

以下是错误:

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) int __cdecl mosqpp::lib_init(void)" (__imp_?lib_init@mosqpp@@YAHXZ) referenced in function main C:\dir\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) int __cdecl mosqpp::lib_cleanup(void)" (__imp_?lib_cleanup@mosqpp@@YAHXZ) referenced in function main IoTSmartTraffic C:\dir\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) public: int __cdecl mosqpp::mosquittopp::reconnect(void)" (__imp_?reconnect@mosquittopp@mosqpp@@QEAAHXZ) referenced in function main C:\dir\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) public: int __cdecl mosqpp::mosquittopp::subscribe(int *,char const *,int)" (__imp_?subscribe@mosquittopp@mosqpp@@QEAAHPEAHPEBDH@Z) referenced in function main C:\dir\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "__declspec(dllimport) public: int __cdecl mosqpp::mosquittopp::loop(int,int)" (__imp_?loop@mosquittopp@mosqpp@@QEAAHHH@Z) referenced in function main C:\dir\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __cdecl mqtt_client::mqtt_client(char const *,char const *,int)" (??0mqtt_client@@QEAA@PEBD0H@Z) referenced in function main C:\dir\main.obj 1

标签: c++mqtt

解决方案


推荐阅读