gpt4 book ai didi

c++ - 在 OMNeT++ 模拟中使用 Paho MQTT

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:26 25 4
gpt4 key购买 nike

我正在将 Paho MQTT C++ 库集成到 OMNeT++ 实现中。我复制了源目录并使用项目的自定义 make 文件在 OMNeT++ 中构建 C 和 C++ 库。我正在使用一些测试代码,它使用了显示的代码 here :

#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 并从此目录遍历到正确的库文件夹。

关于c++ - 在 OMNeT++ 模拟中使用 Paho MQTT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com