Skip to content

Add Windows platform support with complete abstraction layer#122

Closed
Copilot wants to merge 244 commits into
masterfrom
copilot/add-mqtt-support-windows
Closed

Add Windows platform support with complete abstraction layer#122
Copilot wants to merge 244 commits into
masterfrom
copilot/add-mqtt-support-windows

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 11, 2026

The mqttclient library had no Windows platform adaptation layer. All platform-specific code (threading, mutexes, sockets, timers) was Linux-only with pthread dependencies.

Platform Abstraction Layer

Created platform/windows/ with Win32 API implementations:

  • Memory: standard C allocation
  • Mutex: CRITICAL_SECTION
  • Thread: CreateThread with event-based lifecycle
  • Timer: QueryPerformanceCounter for microsecond precision
  • Network: Winsock2 with thread-safe initialization

Automatic Platform Detection

Fixed critical constant definition order bug:

The platform constants (SALOF_USING_WINDOWS, etc.) were defined in salof_defconfig.h after including salof_config.h, but the detection code in salof_config.h needed those constants. This caused all Windows builds to be misdetected as Linux.

// Before (broken): Constants defined too late
#include "salof_config.h"
#define SALOF_USING_WINDOWS 5  // Used by code above!

// After (fixed): Constants defined first
#define SALOF_USING_WINDOWS 5
#include "salof_config.h"

Implemented three-layer detection:

  1. salof_config.h: Primary detection via _WIN32, __linux__, etc.
  2. mqtt_config.h: Fallback with #ifndef guard to prevent redefinition
  3. salof_defconfig.h: Final safety check with error on unknown platform

Build System Updates

  • Platform-specific compiler flags (MSVC /W3 /O2 vs GCC -Wall -O3)
  • Correct static library extensions (.lib on Windows vs .a on Linux)
  • Automatic platform directory selection in CMake
  • Updated minimum CMake version to 3.10

Windows-Specific Issues Fixed

Winsock conflicts: Added WIN32_LEAN_AND_MEAN and _WINSOCKAPI_ guards to ensure winsock2.h takes precedence over old winsock.h

TLS disabled: mbedtls compilation on Windows has extensive issues. Disabled TLS entirely on Windows (TCP-only mode).

Cross-platform examples: All examples now use conditional compilation:

#if defined(_WIN32)
    #include <windows.h>
    #define sleep(x) Sleep((x)*1000)
    typedef HANDLE pthread_t;
#else
    #include <pthread.h>
#endif

Testing

Linux builds verified - no regressions. Windows builds require user testing in actual Windows environment.

Original prompt

This section details on the original issue you should resolve

<issue_title> platform 目录下找不到适配 windows 的文件</issue_title>
<issue_description>公司项目是使用 VS Studio C++开发,需要增加 mqtt 通信功能,看软件介绍可以支持windows,但在 platform 目录下却找不到适配 windows 的文件。是要自己来写windows平台的适配函数,还是有什么方法?
</issue_description>

<agent_instructions>实现以下windows中适配的代码
</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@jiejieTop 目前未适配Windows,您这边可以自己写一个platform

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…lude on Windows

Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Copilot AI changed the title Add Windows platform support with native Win32 APIs Add compile-time platform detection to salof logging system Feb 11, 2026
Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Copilot AI changed the title Add compile-time platform detection to salof logging system Fix system header includes in salof_defconfig.h Feb 11, 2026
Copilot AI and others added 2 commits February 11, 2026 04:56
…tection

Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Copilot AI changed the title Fix system header includes in salof_defconfig.h Add Windows platform support for mqttclient Feb 11, 2026
…form handling

Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Copilot AI changed the title Add Windows platform support for mqttclient Add Windows platform support with complete abstraction layer Feb 11, 2026
Copilot AI and others added 2 commits February 11, 2026 05:09
…orm detection

Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
Co-authored-by: maikebing <3445167+maikebing@users.noreply.github.com>
@maikebing maikebing closed this Feb 11, 2026
@maikebing maikebing force-pushed the copilot/add-mqtt-support-windows branch from 1471619 to 20175e2 Compare February 11, 2026 08:29
@maikebing maikebing deleted the copilot/add-mqtt-support-windows branch February 11, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

platform 目录下找不到适配 windows 的文件

8 participants