Question

I'm trying to assemble a simple "Hello world" application with Masm32. It assembles fine but when I try to link it, the linker says

LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup prog1.exe : fatal error LNK1120: 1 unresolved externals

This is the source code of the program:

.586P

.MODEL FLAT, STDCALL
STD_OUTPUT_HANDLE equ -11

; Prototypes of external procedures
EXTERN GetStdHandle@4:NEAR
EXTERN WriteConsoleA@20:NEAR
EXTERN ExitProcess@4:NEAR

; INCLUDELIB directives for the linker
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

;============ data segment =================
_DATA SEGMENT
HANDL DWORD ?
BUFER DB "Hello world\n", 0
NUMB  DWORD ?
NUMW  DWORD ?
_DATA ENDS

_TEXT SEGMENT
MAIN:
;====== Get the output handle ======
     PUSH STD_OUTPUT_HANDLE
     CALL GetStdHandle@4
     MOV  HANDL, EAX


; Output the buffer contents to the console
     PUSH 0
     PUSH OFFSET NUMW
     PUSH NUMB
     PUSH OFFSET BUFER
     PUSH HANDL
     CALL WriteConsoleA@20

;Exit application
     PUSH 0
     CALL ExitProcess@4
_TEXT ENDS
END

I found in some forums that this is caused by the encode type. However it doesn't seem to matter to my problem

Was it helpful?

Solution

The linker assumes the default name for entry point. You have a few options.
1. Use the C libraries on the platform, which because you're using MASM, I assume you don't want to.
2. Rename your MAIN to _WinMainCRTStartup
3. Use "-entry:MAIN" on the Link.exe command-line (you may need a "public MAIN" line)

OTHER TIPS

You have 2 Options:

  1. Rename your MAIN to _WinMainCRTStartup
  2. Set Windows (/SUBSYSTEM:WINDOWS) in Properties/Configuration/Linker -> SubSystem option.

You are missing the label after the end statement. It should be the same label that the code segment was labeled with, in your case Main. So instead of your last line being:

END

change it to

END MAIN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top