6

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 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

This 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 ).

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 compile the program on windows command prompt.

nasm -f win64 -o hello_world.obj hello_world.asm

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

And to link into an executable.

link hello_world.obj /subsystem:console /entry:main /out:hello_world_basic.exe "KERNEL32.LIB"

What is the problem? Is there anything I missed?

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
jtxkopt
  • 347
  • 1
  • 3
  • 14
  • 1
    [Microsoft has moved some standard C stuff into another library](https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015), namely `legacy_stdio_definitions.lib`. Make sure you link with that properly and you also might need to prepend a leading underscore – Jester Oct 18 '20 at 12:44
  • @Jester I added the prefix _ to printf, but it didn't fix the problem. Also I changed the command line arguments as `link hello_world.obj /subsystem:console /entry:main /out:hello_world_basic.exe "KERNEL32.LIB" "UCRT.LIB" "legacy_stdio_definitions.lib"` – jtxkopt Oct 18 '20 at 13:07
  • 1
    @Jester Thanks. I solved the problem. – jtxkopt Oct 18 '20 at 14:30
  • 1
    IIRC, 64-bit Windows doesn't prepend a leading _ to C symbol names, only 32-bit Windows does that. – Peter Cordes Oct 18 '20 at 15:54

2 Answers2

6

Managed to solve this after reading the answer from Darran Rowe in this discussion. He also explains a bit more why these things need to be done.

Here's the solution:

Run the link command in x64 Native Tools Command Prompt for VS 20XX that you can launch from Start menu under Visual Studio 20XX (Developer Command Prompt for VS 20XX suggested in the tutorial doesn't work).

Use this as the link command:

link hello_world.obj /subsystem:console /out:hello_world_basic.exe kernel32.lib legacy_stdio_definitions.lib msvcrt.lib

The changes here are:

  • /entry:main has been removed
  • kernel32.lib legacy_stdio_definitions.lib msvcrt.lib have been added
Hemaolle
  • 1,680
  • 15
  • 17
5

According to the link Microsoft has moved some standard C stuff into another library @Jester shared.

The definitions of all of the printf and scanf functions have been moved inline into <stdio.h>, <conio.h>, and other CRT headers. This breaking change leads to a linker error (LNK2019, unresolved external symbol) for any programs that declared these functions locally without including the appropriate CRT headers.If possible, you should update the code to include the CRT headers (that is, add #include <stdio.h>) and the inline functions, but if you do not want to modify your code to include these header files, an alternative solution is to add an additional library to your linker input, legacy_stdio_definitions.lib.

Thus you need to link legacy_stdio_definitions.lib for the implementation of printf and also need to initialize CRT so change source code to,

bits 64
default rel

segment .data
    msg db "Hello world!", 0xd, 0xa, 0

segment .text
global main
extern ExitProcess
extern _CRT_INIT

extern printf

main:
    push    rbp
    mov     rbp, rsp
    sub     rsp, 32

    call    _CRT_INIT

    lea     rcx, [msg]
    call    printf

    xor     rax, rax
    call    ExitProcess

And finally, run the linker as follow.

link hello_world.obj /subsystem:console /entry:main /out:hello_world_basic.exe kernel32.lib legacy_stdio_definitions.lib  msvcrt.lib
jtxkopt
  • 347
  • 1
  • 3
  • 14