title: Unresolved external symbol printf in Windows x64 Assembly Programming with NASM
body: <p>I've been trying to learn assembly lately, and came across this <a href="https://sonictk.github.io/asm_tutorial/#introduction/settingup/netwideassembler" rel="nofollow noreferrer">post</a>. 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:</p>
  <pre><code>hello_world.obj : error LNK2001: unresolved external symbol printf
  hello_world_basic.exe : fatal error LNK1120: 1 unresolved external
  </code></pre>
  <p>The above is the output of <code>Microsoft Linker</code> (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 ).</p>
  <p>Here is the sample program used as <strong>&quot;Hello World&quot;</strong> assembly program.</p>
  <p><em>hello_world.asm</em>:</p>
  <pre><code>bits 64
  default rel
  
  segment .data
     msg db &quot;Hello world!&quot;, 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
  
  </code></pre>
  <p>To reproduce the issue, execute the commands respectively.</p>
  <p><strong>1) To compile the program on windows command prompt.</strong></p>
  <pre><code>nasm -f win64 -o hello_world.obj hello_world.asm
  </code></pre>
  <p><strong>2) To set LIB environment variable.</strong></p>
  <pre><code>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
  </code></pre>
  <p><strong>3) And to link into an executable.</strong></p>
  <pre><code>link hello_world.obj /subsystem:console /entry:main /out:hello_world_basic.exe &quot;KERNEL32.LIB&quot;
  </code></pre>