This directory contains the Windows platform adaptation layer for the mqttclient library.
The Windows platform implementation provides the necessary abstractions for:
- Memory allocation (
platform_memory.c/h) - Mutex/synchronization (
platform_mutex.c/h) - Threading (
platform_thread.c/h) - Timers (
platform_timer.c/h) - Network sockets (
platform_net_socket.c/h)
- Windows Vista or later (Windows 7, 8, 10, 11, Server 2008+)
- Visual Studio 2013 or later (recommended: Visual Studio 2019 or 2022)
- CMake 3.10 or later
- Windows SDK (included with Visual Studio)
-
Open a Visual Studio Command Prompt (Developer Command Prompt)
-
Navigate to the project root directory:
cd /d C:\path\to\mqttclient -
Create a build directory:
mkdir build cd build
-
Generate Visual Studio project files:
cmake ..
Or for a specific Visual Studio version:
cmake -G "Visual Studio 16 2019" .. -
Build the project:
cmake --build . --config Release
Or open the generated
.slnfile in Visual Studio and build from the IDE.
-
Ensure MinGW is installed and in your PATH
-
Create a build directory:
mkdir build cd build
-
Generate Makefiles:
cmake -G "MinGW Makefiles" .. -
Build:
mingw32-make
Windows implementation uses Critical Sections (CRITICAL_SECTION) which provide the fastest synchronization for threads within the same process.
Windows threads are created using CreateThread() API. The implementation includes:
- Thread creation with custom stack size
- Thread synchronization using events
- Proper thread cleanup
High-resolution timing is implemented using:
QueryPerformanceCounter()for high-precision timingQueryPerformanceFrequency()for timer calibration- Hybrid sleep approach for
platform_timer_usleep():- Short sleeps (< 1ms): busy-wait for accuracy
- Longer sleeps:
Sleep()+ busy-wait remainder
Windows Sockets (Winsock2) implementation:
- Automatic Winsock initialization (WSAStartup)
- Support for TCP protocol (UDP also available)
- IPv4 and IPv6 support
- Blocking and non-blocking socket modes
- Timeout support for send/receive operations
Note on TLS/SSL Support:
- TLS/SSL (mbedtls) is disabled on Windows by default to avoid compilation issues
- Windows builds use TCP-only connections (no encryption)
- This means MQTT connections on Windows will be unencrypted
- For production use requiring encryption, consider using a TLS-terminating proxy or VPN
- Linux builds continue to support TLS/SSL via mbedtls
When building applications using this library on Windows, you must link against:
ws2_32.lib- Windows Sockets 2 library
Example compiler command:
cl.exe your_app.c mqttclient.lib ws2_32.lib-
Winsock Initialization: The network socket implementation automatically initializes Winsock when needed. No manual initialization is required by the application.
-
Winsock Cleanup: For proper resource cleanup, call
platform_net_socket_cleanup()at program exit. Example:#include "platform_net_socket.h" int main() { // Your application code here // Clean up before exit platform_net_socket_cleanup(); return 0; }
-
Thread Priority: The Windows implementation currently ignores the
priorityparameter inplatform_thread_init(). Windows thread priorities can be set separately if needed usingSetThreadPriority(). -
Stack Size: Thread stack size is respected in the Windows implementation. Default is 0, which uses the system default (typically 1MB).
After building, you can run the example programs:
cd build\bin\Release
onenet_example.exeNote: Some example programs may have compilation issues unrelated to the Windows platform support.
The following issues have been fixed to support Windows builds:
- Compiler Flags: CMake now uses MSVC-compatible flags (
/W3,/O2) instead of GCC flags on Windows - Logging System: Added Windows arch implementation for the salof logging system
- Thread API: Implemented Windows-native threading using CRITICAL_SECTION, Windows Semaphore, and CreateThread
- Weak Functions: Fixed
platform_timer_now()weak function to work with MSVC using/alternatenamelinker directive - Platform Detection: Automatically detect Windows platform and use SALOF_USING_WINDOWS configuration
- Windows SDK Headers: Fixed header include order to prevent Winsock2 conflicts
- Added
WIN32_LEAN_AND_MEANand_WINSOCKAPI_definitions - Ensured
winsock2.his included beforewindows.h - Fixes
IPPROTO_IPV6undeclared andICMP_ERROR_INFOsyntax errors
- Added
- TLS/SSL Disabled on Windows: Due to mbedtls compilation issues on Windows
- TLS support (
nettype_tls.c) is automatically disabled on Windows - Windows builds use TCP-only connections (no encryption)
- This eliminates numerous mbedtls-related compilation errors
- Controlled by
MQTT_NETWORK_TYPE_NO_TLSinconfig/mqtt_config.h - Linux builds continue to support TLS via mbedtls
- TLS support (
- Logging Arch Selection: Fixed CMakeLists.txt to select correct arch implementation
common/log/CMakeLists.txtnow selects Windows or Linux arch directory based on platform- Prevents compilation of Linux pthread code on Windows
- Eliminates
pthread_t,pthread_mutex_t,sem_tundeclared identifier errors
- Static Library Linking: Fixed CMakeLists.txt files to use platform-specific extensions
- Windows:
.libextension for static libraries - Linux:
.aextension for static libraries - Eliminates linker errors "cannot open input file mc-log.lib"
- Applied to all CMakeLists.txt files (common, platform, network, mbedtls, mqttclient)
- Windows:
- Signed/Unsigned Warning: Fixed comparison warning in
common/random.c- Added explicit cast to eliminate signed/unsigned mismatch warning in loop comparison
- Example Platform Support: Fixed examples to work on Windows
- Added conditional includes for Windows (windows.h) vs Linux (unistd.h, pthread.h)
- Platform-specific compiler flags in example CMakeLists.txt
- Windows uses Sleep() and CreateThread(), Linux uses sleep() and pthread
- SALOF Platform Constants: Fixed definition order in
salof_defconfig.h- Platform constants (SALOF_USING_WINDOWS, etc.) now defined before including salof_config.h
- Ensures mqtt_config.h can use these constants when setting SALOF_OS
- Added fallback platform detection for when SALOF_OS isn't defined by config
- Fallback detects Windows via _WIN32, _WIN64, CYGWIN, or _MSC_VER macros
- Fixes examples failing to build due to pthread.h include on Windows
- Automatic Platform Detection in common/log: Enhanced logging system with independent platform detection
salof_config.hnow automatically detects platform at compile time- Supports Windows, Linux, macOS, FreeRTOS, RT-Thread, TencentOS
- No manual SALOF_OS configuration needed - platform is detected automatically
- Detection uses compiler-defined macros (_WIN32, linux, etc.)
- Works independently of MQTT configuration
- See
common/log/README.mdfor detailed documentation
- Fixed pthread.h Include Issues: Corrected system header syntax
- Changed
#include "pthread.h"to#include <pthread.h>in salof_defconfig.h - Changed
#include "memory.h"to#include <string.h>(correct standard header) - System headers should use angle brackets, not quotes
- Changed
- Fixed SALOF_OS Redefinition Conflict: Prevented platform detection override
mqtt_config.hnow checks ifSALOF_OSis already defined before defining it- Added
#ifndef SALOF_OSguard to respect salof_config.h detection - Establishes proper detection priority: salof_config.h → mqtt_config.h → salof_defconfig.h
- Eliminates redefinition that was causing Windows to be detected as Linux
- Ensures Windows builds use
<windows.h>instead of attempting to include<pthread.h> - Fixes: "error C1083: 无法打开包括文件: 'pthread.h': No such file or directory"
If you encounter errors like:
error C2065: "IPPROTO_IPV6": 未声明的标识符- Syntax errors in
ws2tcpip.hrelated toICMP_ERROR_INFO
These are caused by incorrect Windows header include order. The fix ensures:
WIN32_LEAN_AND_MEANis defined to minimize Windows header dependencies_WINSOCKAPI_prevents old winsock1 from being includedwinsock2.handws2tcpip.hare included beforewindows.h
If you encounter errors like:
error C2375: "WSAAsyncGetServByPort": 重定义;不同的链接- Similar errors for other WSA functions
These occur when both winsock.h and winsock2.h are included. The fix:
- Network source files (
nettype_tcp.c,network.c) define_WINSOCKAPI_before any includes - This prevents
windows.hfrom automatically including the oldwinsock.h - Ensures only
winsock2.his used throughout the codebase
If you encounter network-related errors, check:
- Windows Firewall settings
- Antivirus software blocking network connections
- Network proxy settings
- Ensure Windows SDK is properly installed
- Check that
winsock2.handws2_32.libare available - Verify CMake can find the C compiler
Tested on:
- Windows 10 (x64)
- Windows 11 (x64)
- Visual Studio 2019
- Visual Studio 2022
This code follows the same Apache License v2.0 as the main mqttclient project.