如何使用libmosquitto进行编译 [英] How to compile using libmosquitto
问题描述
我正在尝试编译libmosquitto网站(底部)上提供的代码示例: http://mosquitto.org/man/libmosquitto-3.html
Iam trying to compile the code example available on the libmosquitto website (at the bottom): http://mosquitto.org/man/libmosquitto-3.html
我使用Ubuntu 12.04安装了libmosquitto1和libmosquitto1-dev软件包.在安装它们之前,我添加了mosquitto存储库:
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
但是我遇到以下错误:
/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
推荐答案
您必须将-lmosquitto
放在末尾(源文件之后).
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
或更佳:
gcc -Wall -Wextra -pedantic -o mosquito mosquito.c -lmosquitto
这篇关于如何使用libmosquitto进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!