I've been trying to learn assembly lately, and came across this post. The author used NASM and Microsoft linker to set up the assembly working environment. I followed the same steps and installed NASM. Then I started to compile the hello world application. The compilation is successful, but I get an error at the link stage. The error is as follows:
hello_world.obj : error LNK2001: unresolved external symbol printf
hello_world_basic.exe : fatal error LNK1120: 1 unresolved external
The above is the output of Microsoft Linker
(link.exe). I run the link commands from Developer Command Prompt as described in the post, and because hello world is a 64-bits application I set the LIB environment variable correctly (even though not mentioned on the post ).
Here is the sample program used as "Hello World" assembly program.
hello_world.asm:
bits 64
default rel
segment .data
msg db "Hello world!", 0xd, 0xa, 0
segment .text
global main
extern ExitProcess
extern printf
main:
push rbp
mov rbp, rsp
sub rsp, 32
lea rcx, [msg]
call printf
xor rax, rax
call ExitProcess
To reproduce the issue, execute the commands respectively.
1) To compile the program on windows command prompt.
nasm -f win64 -o hello_world.obj hello_world.asm
2) To set LIB environment variable.
set LIB=LIB=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\ATLMFC\lib\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x86;C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64
3) And to link into an executable.
link hello_world.obj /subsystem:console /entry:main /out:hello_world_basic.exe "KERNEL32.LIB"