Skip to content

Fix MinGW linker subsystem flag generation for Qt applications#7

Merged
waruqi merged 3 commits intodevfrom
copilot/fix-6
Sep 16, 2025
Merged

Fix MinGW linker subsystem flag generation for Qt applications#7
waruqi merged 3 commits intodevfrom
copilot/fix-6

Conversation

Copy link
Copy Markdown

Copilot AI commented Sep 16, 2025

The Windows subsystem rule was generating incorrect linker flags for MinGW, causing Qt widget applications to fail compilation with:

ld.exe: unrecognised emulation mode: windows
Supported emulations: i386pep i386pe

The issue occurred because xmake was passing -Wl,-mwindows to MinGW's ld.exe, which interprets -mwindows as an emulation mode (similar to -mi386pep) rather than a subsystem flag. The solution is to pass -mwindows directly without the -Wl, wrapper.

Changes Made

Modified /xmake/rules/platform/windows/subsystem/xmake.lua to use the correct flag format:

elseif target:has_tool("ld", "gcc", "gxx") then
    target:add("ldflags", "-m" .. subsystem, { force = true })

Example

For a Qt widget application like:

target("myapp")
    add_rules("qt.widgetapp")
    add_files("src/*.cpp")

Before (broken):

x86_64-w64-mingw32-g++ ... -Wl,-mwindows

After (working):

x86_64-w64-mingw32-g++ ... -mwindows

This simple change removes the -Wl, wrapper that was causing the emulation mode confusion while maintaining full backward compatibility with all platforms.

Fixes #6.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: waruqi <151335+waruqi@users.noreply.github.com>
Copilot AI changed the title [WIP] xmake在mingw下无法编译 Fix MinGW linker subsystem flag generation for Qt applications Sep 16, 2025
Copilot AI requested a review from waruqi September 16, 2025 03:40
if target:plat() == "mingw" then
target:add("ldflags", "--subsystem," .. subsystem, { force = true })
else
target:add("ldflags", "-m" .. subsystem, { force = true })
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不用改,只处理 gcc

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the ld section changes as requested. Only modified the gcc section. (commit e1c7e71)

if target:plat() == "mingw" then
target:add("ldflags", "-Wl,--subsystem," .. subsystem, { force = true })
else
target:add("ldflags", "-Wl,-m" .. subsystem, { force = true })
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用分平台,直接使用 "-m" .. subsystem ,不要加 -Wl,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use -m + subsystem directly without -Wl, wrapper and removed platform detection as suggested. (commit e1c7e71)

@waruqi waruqi requested a review from Copilot September 16, 2025 03:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes MinGW linker subsystem flag generation for Qt applications by addressing incorrect flag format that caused compilation failures. The issue was that xmake was passing -Wl,-mwindows to MinGW's ld.exe, which interprets -mwindows as an emulation mode rather than a subsystem flag.

  • Adds MinGW platform detection to generate correct --subsystem,windows flags instead of -mwindows
  • Maintains backward compatibility with all other platforms
  • Fixes compilation errors for Qt widget applications on MinGW

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

target:add("ldflags", "-m" .. subsystem, { force = true })
end
end
end
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closing end statement appears to be unmatched. The function structure shows this end doesn't correspond to any opening statement in the visible diff, which will cause a syntax error.

Copilot uses AI. Check for mistakes.
@waruqi
Copy link
Copy Markdown
Owner

waruqi commented Sep 16, 2025

@copilot

@Issues-translate-bot
Copy link
Copy Markdown

Bot detected the issue body's language is not English, translate it automatically.


@copilot

…ctly

Co-authored-by: waruqi <151335+waruqi@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Sep 16, 2025

@copilot

Applied the feedback - simplified to use -mwindows directly without -Wl, wrapper and removed platform-specific handling. (commit e1c7e71)

Copilot AI requested a review from waruqi September 16, 2025 03:56
@waruqi waruqi marked this pull request as ready for review September 16, 2025 03:58
@waruqi waruqi merged commit 9b06d86 into dev Sep 16, 2025
@waruqi waruqi deleted the copilot/fix-6 branch September 16, 2025 03:59
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.

xmake在mingw下无法编译

4 participants