簡體   English   中英

在OMNeT ++仿真中使用Paho MQTT

[英]Using Paho MQTT in OMNeT++ Simulation

我正在將Paho MQTT C ++庫集成到OMNeT ++實現中。 我復制了源目錄,並使用了項目的自定義生成文件來構建OMNeT ++中的C和C ++庫。 我正在使用一些使用此處顯示的代碼的測試代碼:

#include "MQTTConnector.h"

Define_Module(MQTTConnector);

const string SERVER_ADDRESS { "tcp://localhost:1883" };
const string CLIENT_ID      { "sync_client" };
const string TOPIC          { "hello" };

const int  QOS = 1;

class callback : public virtual mqtt::callback
{
    mqtt::client& cli_;

    void connected(const std::string& cause) override {
    std::cout << "\nConnected: " << cause << std::endl;
    cli_.subscribe(TOPIC, QOS);
    std::cout << std::endl;
}

void connection_lost(const std::string& cause) override {
    std::cout << "\nConnection lost";
    if (!cause.empty())
        std::cout << ": " << cause << std::endl;
    std::cout << std::endl;
}

void message_arrived(mqtt::const_message_ptr msg) override {
    std::cout << msg->get_topic() << ": " << msg->get_payload_str() << std::endl;
}

void delivery_complete(mqtt::delivery_token_ptr token) override {}

public:
    callback(mqtt::client& cli) : cli_(cli) {}
};

void MQTTConnector::initialize() {

    mqtt::connect_options connOpts;
    connOpts.set_keep_alive_interval(20);
    connOpts.set_clean_session(false);
    connOpts.set_automatic_reconnect(true);

    client = new mqtt::client(SERVER_ADDRESS, CLIENT_ID);

    callback cb(*client);
    this->client->set_callback(cb);

    try {
        cout << "Connecting to the MQTT server..." << flush;
        this->client->connect(connOpts);
        cout << "OK" << endl;
    }
    catch (const mqtt::exception& exc) {
        cerr << exc.what() << endl;
    }
}

首先,我嘗試了異步客戶端,它運行良好。 但是,我寧願使用同步客戶端。 盡管存在這樣的錯誤,但是我仍然將以下所需的庫與選項-lpaho-mqtt3c-lpaho-mqttpp3鏈接在一起

 Undefined symbols for architecture x86_64:
  "mqtt::client::client(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, mqtt::iclient_persistence*)", referenced from:
      MQTTConnector::initialize() in MQTTConnector.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

知道這里有什么問題嗎?

編輯:我只是試圖以常規方式在我的機器(macOS 10.14)上編譯和安裝庫。 不幸的是,這不會改變行為。

我通過將絕對路徑設置為鏈接器標志來修復它。 在我為MQTT功能在.oppfeatures文件中相對地設置-L之前。 解決方案是將其設置為pwd並從該目錄遍歷到正確的庫文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM