Windows端编译MQTT-C源码,获取mqttc库(动态库与静态库),用于集成到Qt工程中使用mqtt订阅与发布功能。
编译源码与编译出来的mqttc动态库、静态库下载https://download.csdn.net/download/qq_38159549/89730367https://download.csdn.net/download/qq_38159549/89730367
编译环境:Windows10 + vs2019
MQTT-C源码:https://github.com/LiamBindle/MQTT-C
参考文章:移植MQTT-C库(附源码)-CSDN博客 、【mqtt】编译使用 MQTT-C-CSDN博客
一、下载MQTT-C源码
git clone https://github.com/LiamBindle/MQTT-C.git
如果遇到git clone Timed out,修改Git的网络设置(https://zhuanlan.zhihu.com/p/636418854)
# 注意修改成自己的IP和端口号
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
二、修改MQTT-C源码
因为Windows端cmake编译examples报错,所以屏蔽examples部分。
1.屏蔽CMakeLists文件examples部分:
/*
* CMakeLists.txt文件
*/
# 屏蔽EXAMPLES 程序生成
# option(MQTT_C_EXAMPLES "Build MQTT-C examples?" ON)
option(MQTT_C_EXAMPLES "Build MQTT-C examples?" OFF)
2. 屏蔽makefile文件examples部分:
/*
* makefile文件
*/
all: $(BINDIR) $(MQTT_C_UNITTESTS)
# 取消示例程序生成,只保留库和单元测试
# all: $(BINDIR) $(MQTT_C_UNITTESTS) $(MQTT_C_EXAMPLES)
# bin/simple_%: examples/simple_%.c $(MQTT_C_SOURCES)
# $(CC) $(CFLAGS) $^ -lpthread $(MSFLAGS) -o $@
# bin/reconnect_%: examples/reconnect_%.c $(MQTT_C_SOURCES)
# $(CC) $(CFLAGS) $^ -lpthread $(MSFLAGS) -o $@
# bin/bio_%: examples/bio_%.c $(MQTT_C_SOURCES)
# $(CC) $(CFLAGS) `pkg-config --cflags openssl` -D MQTT_USE_BIO $^ -lpthread $(MSFLAGS) `pkg-config --libs openssl` -o $@
# bin/openssl_%: examples/openssl_%.c $(MQTT_C_SOURCES)
# $(CC) $(CFLAGS) `pkg-config --cflags openssl` -D MQTT_USE_BIO $^ -lpthread $(MSFLAGS) `pkg-config --libs openssl` -o $@
3.同时生成mqttc.dll动态库与mqttc.lib静态库,因为MQTT-C CMakeLists源码中只生成了静态库lib,所以需要修改CMakeLists代码,同时生成mqttc.dll动态库与mqttc.lib静态库。
/*
* CMakelists.txt文件
*/
# MQTT-C library
# 生成静态库
add_library(mqttc STATIC
src/mqtt_pal.c
src/mqtt.c
)
target_include_directories(mqttc PUBLIC include)
target_link_libraries(mqttc PUBLIC
$<$<C_COMPILER_ID:MSVC>:ws2_32>
)
# 生成动态库
add_library(mqttc_shared SHARED
src/mqtt_pal.c
src/mqtt.c
)
# 将动态库名称修改为 mqttc
set_target_properties(mqttc_shared PROPERTIES OUTPUT_NAME "mqttc")
target_include_directories(mqttc_shared PUBLIC include)
target_link_libraries(mqttc_shared PUBLIC
$<$<C_COMPILER_ID:MSVC>:ws2_32>
)
三、cmake编译源码(Windows )
源码文件夹下打开 Powershell终端
1.使用 cmake
生成 Visual Studio 的项目文件:
cmake -S . -B build -G "Visual Studio 16 2019"
2.然后通过 cmake
构建 Release
版本:
cmake --build build --config Release
运行结果:
###### 输入命令 cmake -S . -B build -G "Visual Studio 16 2019"
PS F:\Custom-Project\Project-Tra\2024\Mqtt-cmake\cmake-mqtt-c-Test\MQTT-C> cmake -S . -B build -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.10240.0 to target Windows 10.0.19045.
-- Configuring done (0.0s)
-- Generating done (0.1s)
-- Build files have been written to: F:/Custom-Project/Project-Tra/2024/Mqtt-cmake/cmake-mqtt-c-Test/MQTT-C/build
###### 输入命令 cmake --build build --config Release
PS F:\Custom-Project\Project-Tra\2024\Mqtt-cmake\cmake-mqtt-c-Test\MQTT-C> cmake --build build --config Release
>>
CMake is re-running because F:/Custom-Project/Project-Tra/2024/Mqtt-cmake/cmake-mqtt-c-Test/MQTT-C/build/CMakeFiles/generate.stamp is out-of-date.
the file 'F:/Custom-Project/Project-Tra/2024/Mqtt-cmake/cmake-mqtt-c-Test/MQTT-C/CMakeLists.txt'
is newer than 'F:/Custom-Project/Project-Tra/2024/Mqtt-cmake/cmake-mqtt-c-Test/MQTT-C/build/CMakeFiles/generate.stamp.depend'
result='-1'
-- Selecting Windows SDK version 10.0.10240.0 to target Windows 10.0.19045.
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: F:/Custom-Project/Project-Tra/2024/Mqtt-cmake/cmake-mqtt-c-Test/MQTT-C/build
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版权所有(C) Microsoft Corporation。保留所有权利。
mqttc.vcxproj -> F:\Custom-Project\Project-Tra\2024\Mqtt-cmake\cmake-mqtt-c-Test\MQTT-C\build\Release\mqttc.lib
mqttc_shared.vcxproj -> F:\Custom-Project\Project-Tra\2024\Mqtt-cmake\cmake-mqtt-c-Test\MQTT-C\build\Release\mqttc.dll
四、总结
编译完MQTT-C源码,获得mqttc.dll动态库与mqttc.lib静态库(位于 ..\MQTT-C\build\Release文件夹下),另外一篇文章演示在Qt工程使用mqttc库,并与mqtt服务器进行数据通信。