写了一个mqtt的client程序,编译时报错如何解决?

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <vector>
#include <mutex>

#include "MQTTAsync.h"
#define PUB_MQTT_TOPIC           "service2wuxi"
#define PUB_MQTT_CLIENTID        "SERVICE_PUB_MQTT"
#define QOS                       0
#define KEY_MQTT_BROKER_ADDRESS   "172.0.0.1"
#define KEY_MQTT_BROKER_PORT     1883
#define BUFFER_SIZE              1024
int main()
{
    int ret;
    char input_msg[BUFFER_SIZE];
    char recv_msg[BUFFER_SIZE] = "hello this msg is send from yancheng! ";
    char address[] = "tcp://localhost:1883";
    //1.创建一个客户端对象
    MQTTAsync pub_client;
    //初始化MQTT Client选项
    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    //声明消息token
    //MQTTClient_deliveryToken token;   
    //使用参数创建一个client,并将其赋值给之前声明的client
 MQTTAsync_create(&pub_client, address, PUB_MQTT_CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);


    //MQTTAsync pub_client_ = client;
    MQTTAsync_setCallbacks(pub_client, NULL, NULL, NULL, NULL);
    //2.设置连接MQTT服务器的选项
    conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        //conn_opts.username = USERNAME;
    //conn_opts.password = PASSWORD;
        conn_opts.onSuccess = NULL;
        conn_opts.automaticReconnect = 1;
        conn_opts.context = pub_client;
        MQTTAsync_connectOptions pub_opts_ = conn_opts;
        //使用MQTTClient_connect将client连接到服务器,使用指定的连接选项。成功则返回MQTTCLIENT_SUCCESS
        if ((ret = MQTTAsync_connect(pub_client, &conn_opts)) != MQTTASYNC_SUCCESS)
        {
                printf("Failed to start connect, return code %d\n", ret);
        }
    //初始化MQTT MQTTClient_message选项
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    opts.context = pub_client;
    pubmsg.payload = recv_msg;
    pubmsg.payloadlen = (int)strlen(recv_msg);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;

    if ((ret = MQTTAsync_sendMessage(pub_client, PUB_MQTT_TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
    {
        printf("MQTT sendMsg failed ,errCode-->%d", ret);
    }


    return 0;
}

编译时报错

v2x@ubuntu:~/Desktop/yancheng/mqttClient$ g++ test123.c -o test123 -std=c++11
/tmp/ccT4gVxQ.o: In function `main':
test123.c:(.text+0xf5): undefined reference to `MQTTAsync_create'
test123.c:(.text+0x119): undefined reference to `MQTTAsync_setCallbacks'
test123.c:(.text+0x185): undefined reference to `MQTTAsync_connect'
test123.c:(.text+0x383): undefined reference to `MQTTAsync_sendMessage'
collect2: error: ld returned 1 exit status
网上有人说缺少编译的库文件。

后来我到网上下载了mqtt 的源代码,编译了一下,结果编译出的库文件是arm架构的

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
当然,我可以为您提供一个基本的MQTT Java客户端示例代码。请注意,您需要确保已经安装了Eclipse Paho MQTT库。 ```java import org.eclipse.paho.client.mqttv3.*; public class MqttClientExample { public static void main(String[] args) { String broker = "tcp://mqtt.eclipse.org:1883"; String clientId = "JavaClient"; String topic = "test/topic"; try { MqttClient mqttClient = new MqttClient(broker, clientId); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); mqttClient.connect(connOpts); System.out.println("Connected to MQTT broker"); mqttClient.subscribe(topic); System.out.println("Subscribed to topic: " + topic); mqttClient.setCallback(new MqttCallback() { public void connectionLost(Throwable cause) { System.out.println("Connection lost: " + cause.getMessage()); } public void messageArrived(String topic, MqttMessage message) throws Exception { System.out.println("Received message: " + new String(message.getPayload())); } public void deliveryComplete(IMqttDeliveryToken token) { // Not used in this example } }); } catch (MqttException e) { e.printStackTrace(); } } } ``` 上述示例代码创建了一个MQTT客户端,连接到指定的MQTT代理服务器(`broker`)。然后,它订阅了一个特定的主题(`topic`),并设置了一个回调函数来处理接收到的消息。当您运行此代码,它将连接到MQTT代理并开始接收消息。 请注意,这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。另外,确保在使用此代码之前替换有效的MQTT代理地址、客户端ID和主题。 希望对您有所帮助!如果您有任何其他问题,请随提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值