You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NASM与GCC创建Windows GUI汇编程序时遭遇链接错误求助

解决NASM汇编Win32 GUI程序的GCC链接错误

问题根源

你遇到的undefined reference错误,是因为GCC默认会链接C运行时库(CRT),这会干扰你自定义的_start入口点,同时导致Windows API的stdcall符号无法被正确解析。

解决步骤

  1. 修改链接命令
    在GCC链接时添加-nostdlib参数,禁用CRT链接,让GCC直接使用你的_start作为程序入口。修正后的完整命令:

    nasm -f win32 -o file.obj file.asm
    gcc -m32 -nostdlib -o file.exe file.obj -luser32 -lkernel32
    
  2. 代码优化(可选)
    你代码里的add esp, 16是多余的:MessageBoxA采用stdcall调用约定,函数执行完毕后会自动清理栈上的16字节参数,无需手动处理。删掉这行代码不影响功能,还能精简指令。

修正后的代码示例:

section .data
    caption db 'Hello, Windows!', 0
    message db 'This is a simple GUI application written in assembly.', 0

section .text
    global _start

    extern _MessageBoxA@16, _ExitProcess@4

    _start:
        ; Set up parameters for MessageBoxA function
        push 0        ; hWnd (NULL for message box)
        push message  ; lpText
        push caption  ; lpCaption
        push 0        ; uType (MB_OK)

        ; Call MessageBoxA function
        call _MessageBoxA@16

        ; Exit the process
        push 0
        call _ExitProcess@4

内容的提问来源于stack exchange,提问作者FrostDream

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:37:42