How to compile using libmosquitto

Anjz picture Anjz · Oct 31, 2013 · Viewed 10.2k times · Source

Iam trying to compile the code example available on the libmosquitto website (at the bottom): http://mosquitto.org/man/libmosquitto-3.html

Iam using Ubuntu 12.04 and I've installed libmosquitto1 and libmosquitto1-dev packages. Before installing them I added the mosquitto repository:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update

Iam trying to compile the example as follows:

gcc -lmosquitto mosquito.c -o mosquito

But I get the following errors:

/tmp/cc6eU8kw.o: In function `my_connect_callback':
mosquito.c:(.text+0xf8): undefined reference to `mosquitto_subscribe'
/tmp/cc6eU8kw.o: In function `main':
mosquito.c:(.text+0x298): undefined reference to `mosquitto_lib_init'
mosquito.c:(.text+0x2b4): undefined reference to `mosquitto_new'
mosquito.c:(.text+0x310): undefined reference to `mosquitto_log_callback_set'
mosquito.c:(.text+0x324): undefined reference to `mosquitto_connect_callback_set'
mosquito.c:(.text+0x338): undefined reference to `mosquitto_message_callback_set'
mosquito.c:(.text+0x34c): undefined reference to `mosquitto_subscribe_callback_set'
mosquito.c:(.text+0x364): undefined reference to `mosquitto_connect'
mosquito.c:(.text+0x3b4): undefined reference to `mosquitto_loop'
mosquito.c:(.text+0x3c8): undefined reference to `mosquitto_destroy'
mosquito.c:(.text+0x3d0): undefined reference to `mosquitto_lib_cleanup'
collect2: ld returned 1 exit status

Can someone give me some tips on how to compile this simple example? Thanks

Answer

Runium picture Runium · Oct 31, 2013

You have to put the -lmosquitto at the end (after the source files).

gcc mosquito.c -lmosquitto -o mosquito
# or
gcc mosquito.c -o mosquito -lmosquitto
# or
gcc -o mosquito mosquito.c -lmosquitto

Or better:

gcc -Wall -Wextra -pedantic -o mosquito mosquito.c -lmosquitto