Я пытаюсь инициализировать клиентский сервер на моем ноутбуке для подключения к моему Raspberry Pi с помощью 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