I am integrating the Paho MQTT C++ library into an OMNeT++ implementation. I copied over the source directories and use the custom make files of the projects for building the C and the C++ libraries inside OMNeT++. I am using some test code which uses the code shown 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;
}
}
First, I tried the asynchronous client, which worked fine. However, I rather want to use the synchronous client. With that I get the following error, despite the fact, that I link the needed libraries with the options -lpaho-mqtt3c and -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)
Any idea what is wrong here?
EDIT: I just tried to compile and install the libraries in the regular way on my machine (macOS 10.14). Unfortunately, this does not change the behavior.
I fixed it by setting an absolute path as linker flag. Before I set -L relatively in the .oppfeatures file for my MQTT feature. The solution was to set it to pwd
and traverse to the correct library folder from this directory.