问题
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