1、按上节第一种配置编译时报错,
无法打开文件“paho-mqtt3as.lib”
,主要是没有勾选Static,顺便把SSL也勾选,会更新编译列表选项,
然后再点Configure如果没有错误,红色全部消失、Generate。
2、编译依旧报错,不过已经不影响本次测试了,后面再处理这个问题
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 OpenSSL_version,函数 MQTTClient_getVersionInfo 中引用了该符号 paho-mqtt3cs I:\mqtt\paho.mqtt.c\build\src\MQTTClient.obj 1
原因是我这里用的openssl版本问题
https://slproweb.com/download/Win64OpenSSL-1_1_1d.exe
设置这个路径
之前自动选择的是C:\OpenSSL-Win32下的lib文件
configure,generate即可解决这个问题
3、测试推送
订阅,修改地址,用户名,密码为mqtt学习-1配置
3.1修改I:\mqtt\paho.mqtt.c\src\samples\MQTTAsync_subscribe.c如下
/*******************************************************************************
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "tcp://127.0.0.1:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int disc_finished = 0;
int subscribed = 0;
int finished = 0;
void connlost(void *context, char *cause)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
printf("\nConnection lost\n");
if (cause)
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "admin";
conn_opts.password = "password";
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName);
return 1;
void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
printf("Disconnect failed, rc %d\n", response->code);
disc_finished = 1;
void onDisconnect(void* context, MQTTAsync_successData* response)
printf("Successful disconnection\n");
disc_finished = 1;
void onSubscribe(void* context, MQTTAsync_successData* response)
printf("Subscribe succeeded\n");
subscribed = 1;
void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
printf("Subscribe failed, rc %d\n", response->code);
finished = 1;
void onConnectFailure(void* context, MQTTAsync_failureData* response)
printf("Connect failed, rc %d\n", response->code);
finished = 1;
void onConnect(void* context, MQTTAsync_successData* response)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc;
printf("Successful connection\n");
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
opts.onSuccess = onSubscribe;
opts.onFailure = onSubscribeFailure;
opts.context = client;
if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start subscribe, return code %d\n", rc);
finished = 1;
int main(int argc, char* argv[])
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
int rc;
int ch;
if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
!= MQTTASYNC_SUCCESS)
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
goto exit;
if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
printf("Failed to set callbacks, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start connect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
while (!subscribed && !finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
if (finished)
goto exit;
ch = getchar();
} while (ch!='Q' && ch != 'q');
disc_opts.onSuccess = onDisconnect;
disc_opts.onFailure = onDisconnectFailure;
if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
while (!disc_finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
destroy_exit:
MQTTAsync_destroy(&client);
exit:
return rc;
3.2修改I:\mqtt\paho.mqtt.c\src\samples\MQTTAsync_publish.c如下
/*******************************************************************************
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "tcp://127.0.0.1:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int finished = 0;
void connlost(void *context, char *cause)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "admin";
conn_opts.password = "password";
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
printf("Disconnect failed\n");
finished = 1;
void onDisconnect(void* context, MQTTAsync_successData* response)
printf("Successful disconnection\n");
finished = 1;
void onSendFailure(void* context, MQTTAsync_failureData* response)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;
printf("Message send failed token %d error code %d\n", response->token, response->code);
opts.onSuccess = onDisconnect;
opts.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
void onSend(void* context, MQTTAsync_successData* response)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;
printf("Message with token value %d delivery confirmed\n", response->token);
opts.onSuccess = onDisconnect;
opts.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
void onConnectFailure(void* context, MQTTAsync_failureData* response)
printf("Connect failed, rc %d\n", response ? response->code : 0);
finished = 1;
void onConnect(void* context, MQTTAsync_successData* response)
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
int rc;
printf("Successful connection\n");
opts.onSuccess = onSend;
opts.onFailure = onSendFailure;
opts.context = client;
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = (int)strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
/* not expecting any messages */
return 1;
int main(int argc, char* argv[])
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
printf("Failed to create client object, return code %d\n", rc);
exit(EXIT_FAILURE);
if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
printf("Failed to set callback, return code %d\n", rc);
exit(EXIT_FAILURE);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
printf("Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (!finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
MQTTAsync_destroy(&client);
return rc;
3.3进入输出目录,将paho-mqtt3a.dll拷贝到Debug目录下
说明运行正常
java 浮点数 判断相等
浮点数之间的等值判断,基本数据类型不能使用 == 进行比较,包装数据类型不能使用 equals 进行判断。说明:浮点数采用“尾数+阶码”的编码方式,类似于科学计数法的“有效数字+指数”的表示方式。二进制无法精确表 示大部分的十进制小数。反例: float a = 1.0F - 0.9F;
float b = 0.9F