我试图将hello.asm构建为一个Windows可执行文件,但是当我使用建议的命令来组装、链接和编译代码时,会出现错误。无论我使用的是草莓Perl的GCC还是MinGW的GCC,都给出了相同的YASM/NASM代码的错误。
这是痕迹。要么我在Makefile或.ASM中做错了什么,要么链接过程充满了缺陷。不管怎样,我希望有人能帮我解决错误。
无论我使用nasm还是yasm来组装对象文件,结果都是一样的。
草莓Perl GCC痕迹:
C:\> make
nasm -f win32 -l hello.lst hello.asm
gcc -o hello hello.o
ld: cannot find crt1.o: No such file or directory
ld: cannot find -lmingw32
ld: cannot find -lgcc
ld: cannot find -lmoldname
ld: cannot find -lcrtdll
ld: cannot find -luser32
ld: cannot find -lkernel32
ld: cannot find -ladvapi32
ld: cannot find -lshell32
ld: cannot find -lmingw32
ld: cannot find -lgcc
ld: cannot find -lmoldname
ld: cannot find -lcrtdll
make: *** [hello] Error 1MinGW GCC追踪:
$ make
gcc -o hello hello.o
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0x104): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
make: *** [hello] Error 1规格:
发布于 2012-11-22 07:59:28
将main()函数替换为标签_WinMain@16,如下所示:
main.asm
section .text
extern _foo
;--------------------------------------------------
; main()
; 在 Win32 環境下, _WinMain@16 為程式進入點
;--------------------------------------------------
global _WinMain@16
_WinMain@16:
; foo(2,3)
; 呼叫時, 參數為堆疊順序 (先進後出)
push DWORD 3 ; b=3
push DWORD 2 ; a=2
call _foo
; 堆疊復原
pop eax
pop eax
retfoo.c
#include <stdio.h>
void foo(int a, int b) {
printf("%d + %d = %d\n", a, b, a+b);
}Makefile
all:
rm -f *.o
gcc -c foo.c
nasm -f win32 -o main.o main.asm
gcc -o main.exe main.o foo.o发布于 2012-09-20 18:44:03
几个部分的解决方案:
gcc ...来构建可执行文件时,我得到了很少的链接错误。但是当我使用make运行完全相同的命令时,我得到了各种各样的链接错误。事实证明,自由帕斯卡的make.exe是隐藏适当的make.exe。您可以通过在PATH中提升适当的目录来修复这个问题,或者卸载违规的应用程序。我对Pascal没有多大用处,所以我卸载了它,突然gcc开始更好地工作了。_)。为了保持程序集代码的多平台性,在代码中省略下划线,并使用Makefile选项告诉nasm/yasm在Windows构建时以下划线作为前缀。ld调用来在Windows中将这些东西链接在一起是行不通的。替代gcc修复了奇怪的“未定义的printf”链接错误,因为GCC不知何故知道如何解决这个问题。eax设置为零,然后返回。发布于 2012-09-20 02:29:50
对于一个简单的windows控制台hello: hello.asm
extern printf, ExitProcess
SECTION .data
szHello db "Hello there!", 0
SECTION .text
StartHello:
push szHello
call printf
add esp, 4
push 0
call ExitProcess makefile:
hello: hello.obj
GoLink.exe /console /entry StartHello hello.obj kernel32.dll msvcrt.dll
hello.obj: hello.asm
nasm -f win32 hello.asm -o hello.objhttps://stackoverflow.com/questions/12504868
复制相似问题