How to compile example program using libmosquitto [closed]

若如初见. 提交于 2019-12-11 22:34:38

问题


I am getting the following errors when I try to compile one of the example:

main.cpp:8:undefined reference to `mosqpp::lib_init()'
main.cpp:13: undefined reference to `mosqpp::mosquittopp::loop(int, int)'
main.cpp:15: undefined reference to `mosqpp::mosquittopp::reconnect()'
temperature_conversion.o: In function `mqtt_tempconv':
temperature_conversion.cpp:7: undefined reference to `mosqpp::mosquittopp::mosquittopp(char const*, bool)'
temperature_conversion.cpp:13: undefined reference to `mosqpp::mosquittopp::connect(char const*, int, int)'
temperature_conversion.cpp:7: undefined reference to `mosqpp::mosquittopp::~mosquittopp()'
temperature_conversion.o: In function `mqtt_tempconv::on_connect(int)':
temperature_conversion.cpp:21: undefined reference to `mosqpp::mosquittopp::subscribe(int*, char const*, int)'
temperature_conversion.o: In function `mqtt_tempconv::on_message(mosquitto_message const*)':
temperature_conversion.cpp:37: undefined reference to `mosqpp::mosquittopp::publish(int*, char const*, int, void const*, int, bool)'

Please help


回答1:


Your question doesn't give much detail - you should describe how you've gone about it as well as what the problem is.

Having said that, it looks like you haven't linked against the library.

If you are compiling like this:

g++ main.o temperature_conversion.o -o temperature_conversion 

Then you need to link against the library:

g++ main.o temperature_conversion.o -o temperature_conversion -lmosquittopp

I'm assuming it is already installed.




回答2:


locate libmosquittopp.so can help you understanding the directory to give to g++ as -L parameter.

g++ main.o temperature_conversion.o -o temperature_conversion -lmosquittopp \
    -L$(locate libmosquitto.so | tail -n 1 | xargs -n1 dirname)


来源:https://stackoverflow.com/questions/26334502/how-to-compile-example-program-using-libmosquitto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!