编译C++ Paho MQTT代码报错:找不到Paho MQTT C库
It seems your Paho MQTT C library is installed properly, but the CMake build system for the C++ library can't locate it. This is a common hiccup on Ubuntu 16.04, usually tied to dynamic link cache updates or CMake's default search path limitations. Here are targeted fixes to resolve this:
1. Refresh the system dynamic link cache
When you install libraries to /usr/local/lib (the default location for sudo make install), Ubuntu 16.04 doesn't automatically update its dynamic linker cache. This means the system (and CMake) might not recognize the newly installed Paho C library. Run this command first:
sudo ldconfig
Then try rebuilding the C++ library again, clearing old build artifacts to avoid conflicts:
cd ~/Paho_MQTT/paho_cpp/paho.mqtt.cpp/build rm -rf * cmake .. make
2. Manually specify the Paho C library path to CMake
If the first step doesn't work, CMake might not be checking /usr/local by default. You can explicitly point it to the C library's headers and binaries:
First, confirm the default installation paths (these are correct if you used the standard make install):
- Header files:
/usr/local/include - Library file:
/usr/local/lib/libpaho-mqtt3a.so(shared library) or/usr/local/lib/libpaho-mqtt3a.a(static library)
Run CMake with these paths specified:
cd ~/Paho_MQTT/paho_cpp/paho.mqtt.cpp/build rm -rf * cmake -DPAHO_MQTT_C_INCLUDE_DIR=/usr/local/include -DPAHO_MQTT_C_LIBRARY=/usr/local/lib/libpaho-mqtt3a.so .. make
If you built the static version of the Paho C library, replace .so with .a in the library path.
3. Verify shared library support in Paho C library (optional)
If issues persist, check if you enabled shared library builds when compiling the Paho C library—the C++ library typically expects this by default:
- Navigate back to your Paho C build directory:
cd ~/Paho_MQTT/paho_c/paho.mqtt.c/build - Reopen the CMake configuration:
ccmake .. - Use arrow keys to navigate to
BUILD_SHARED_LIBSand toggle it toON(press Enter to change the value). - Recompile and reinstall the C library:
make sudo make install sudo ldconfig - Return to the C++ library build directory and try compiling again.
内容的提问来源于stack exchange,提问作者Salman

