Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Paho C Mqtt library error in C++ Project

Tags:

c++

c

linker

mqtt

paho

I'm trying to include the MQTT-C-Client-Library in a simple C++ project. I have included the header file succesfully like this #include "MQTTClient.h". Compiling it in the linux terminal was printing this errors:

[xy@localhost mosquittoProject]$ sudo g++ *.cpp -o MQTTTest
/tmp/ccHn3s6m.o: In function `main':
mosquitto_test.cpp:(.text+0x11e): undefined reference to `MQTTClient_create'
mosquitto_test.cpp:(.text+0x13f): undefined reference to `MQTTClient_connect'
collect2: error: ld returned 1 exit status

I figured out that I need to link the library after some googling: Example MQTT Client Code not working C

Based on this question and answer I tried compiling it again like this:

sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest

Which compiles fine but when running I get still an error. Console commands and output:

[xy@localhost mosquittoProject]$ sudo g++ -L/home/xy/Desktop/paho.mqtt.c/build/output/ *.cpp -l paho-mqtt3c -o MQTTTest
[xy@localhost mosquittoProject]$ ./MQTTTest 
./MQTTTest: error while loading shared libraries: libpaho-mqtt3c.so.1: cannot open shared object file: No such file or directory

I replaced the actual username by xy in this post.

What am I doing wrong here?

like image 406
Matthias Herrmann Avatar asked Jan 30 '26 12:01

Matthias Herrmann


1 Answers

The problem looks like the library (libpaho-mqtt3c.so.1) is not on the library path.

It looks like you are linking against the build location of the library and have not installed it to the default system location (e.g. /usr/local/lib) by running sudo make install.

By default on Linux the runtime linker searches the locations listed in /etc/ld.so.conf and /etc/ld.so.conf.d. if you edit these remember to run sudo ldconfig to update the cache.

You can add the location of the library to the LD_LIBRARY_PATH environment variable e.g.:

$ LD_LIBRARY_PATH=/home/xy/Desktop/paho.mqtt.c/build/output/ ./MQTTTest 
like image 174
hardillb Avatar answered Feb 02 '26 01:02

hardillb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!