Windows11下编译Eclipse Paho.mqtt.cpp静态库遇CMake错误求助
问题:Windows 11编译Paho.mqtt.cpp静态库失败(用于QNX项目)
执行的CMake命令
cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c
首次报错信息
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22631. -- Configuring done (0.0s) CMake Error at src/CMakeLists.txt:84 (target_link_libraries): Target "paho-mqttpp3-static" links to: eclipse-paho-mqtt-c::paho-mqtt3a-static but the target was not found. Possible reasons include: * There is a typo in the target name. * A find_package call is missing for an IMPORTED target. * An ALIAS target is missing. -- Generating done (0.1s) CMake Generate step failed. Build files cannot be regenerated correctly.
手动修改CMakeLists.txt后的新错误
添加find_package后出现以下警告:
CMake Warning at src/CMakeLists.txt:29 (find_package): By not providing "FindC:\mqtt\paho-c.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "C:\mqtt\paho-c", but CMake did not find one. Could not find a package configuration file provided by "C:\mqtt\paho-c" with any of the following names: C:\mqtt\paho-cConfig.cmake c:\mqtt\paho-c-config.cmake Add the installation prefix of "C:\mqtt\paho-c" to CMAKE_PREFIX_PATH or set "C:\mqtt\paho-c_DIR" to a directory containing one of the above files. If "C:\mqtt\paho-c" provides a separate development package or SDK, be sure it has been installed. CMake Warning at src/CMakeLists.txt:84 (find_package): By not providing "FindC:\mqtt\paho-c.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "C:\mqtt\paho-c", but CMake did not find one. Could not find a package configuration file provided by "C:\mqtt\paho-c" with any of the following names: C:\mqtt\paho-cConfig.cmake c:\mqtt\paho-c-config.cmake Add the installation prefix of "C:\mqtt\paho-c" to CMAKE_PREFIX_PATH or set "C:\mqtt\paho-c_DIR" to a directory containing one of the above files. If "C:\mqtt\paho-c" provides a separate development package or SDK, be sure it has been installed.
解决办法
问题本质是Paho C库未正确编译安装,导致CMake找不到它的配置文件,按以下步骤操作:
先正确编译安装Paho C库(paho.mqtt.c),不要只解压或克隆仓库:
- 下载/克隆paho.mqtt.c仓库到本地,打开CMD进入仓库根目录
- 依次执行以下命令:
cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-c -DPAHO_BUILD_STATIC=ON -DPAHO_WITH_SSL=OFF cmake --build build --config Release cmake --install build --config Release
执行完成后,
C:\mqtt\paho-c目录下会生成静态库、头文件,以及关键的paho-mqtt-cConfig.cmake配置文件。回到paho.mqtt.cpp仓库根目录,重新执行CMake命令,明确指定编译静态库:
cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DPAHO_BUILD_STATIC=ON -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c不要手动修改paho.mqtt.cpp的
src/CMakeLists.txt,原仓库的配置已经包含正确的依赖查找逻辑,手动添加find_package会破坏原有逻辑。验证配置文件是否存在:检查
C:\mqtt\paho-c\lib\cmake\PahoMqttC目录下是否有paho-mqtt-cConfig.cmake,如果没有,说明Paho C库安装失败,重新执行第一步。
内容的提问来源于stack exchange,提问作者avvocato
相关产品推荐
相关产品推荐

