我正在将 Paho MQTT C++ 库集成到 OMNeT++ 实现中。我复制了源目录并使用项目的自定义 make 文件在 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)上以常规方式编译和安装库。不幸的是,这不会改变行为。