asm_tutorial icon indicating copy to clipboard operation
asm_tutorial copied to clipboard

Linking the first example with the instructions given doesn't work

Open Hemaolle opened this issue 3 years ago • 3 comments

I'm getting these errors with the command in the tutorial:

hello_world.obj : error LNK2001: unresolved external symbol ExitProcess
hello_world.obj : error LNK2001: unresolved external symbol printf
hello_world_basic.exe : fatal error LNK1120: 2 unresolved externals

This command worked for me:

link hello_world.obj /subsystem:console /out:hello_world_basic.exe kernel32.lib legacy_stdio_definitions.lib msvcrt.lib
  • There's no /entry:main
  • Added kernel32.lib legacy_stdio_definitions.lib msvcrt.lib

Also, I had to run this in x64 Native Tools Command Prompt for VS 2017, Developer Command Prompt for VS2017 suggested in the tutorial didn't work.

Hemaolle avatar May 18 '21 18:05 Hemaolle

Hi @Hemaolle: thanks for the report! However, I'm slightly confused:

There's no /entry:main

Maybe it wasn't clear, but the Writing A Build Script section does cover this: https://sonictk.github.io/asm_tutorial/#hello,worldrevisted/writingabuildscript . The example also does specify /entry:main as well: https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L82

Added kernel32.lib legacy_stdio_definitions.lib msvcrt.lib

This is also covered in the same section, and also specified in the example build script: https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L89

Also, I had to run this in x64 Native Tools Command Prompt for VS 2017, Developer Command Prompt for VS2017 suggested in the tutorial didn't work.

That's very strange; what are you seeing when you try to run this in the developer command prompt? For all intents and purposes that should be calling vcvarsall.bat, which is similar to what is being done in the example build script (so that it doesn't require to be run from within a developer command prompt itself): https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L34

Let me know if something in the tutorial was otherwise unclear regarding the above!

sonictk avatar Jul 27 '21 09:07 sonictk

Ah, I suppose I could have been clearer about which section I was talking about. I didn't yet get to the build script part, this was about the "Hello, world" example: https://sonictk.github.io/asm_tutorial/#introduction/settingup/hello,world.

Hi @Hemaolle: thanks for the report! However, I'm slightly confused:

There's no /entry:main

Maybe it wasn't clear, but the Writing A Build Script section does cover this: https://sonictk.github.io/asm_tutorial/#hello,worldrevisted/writingabuildscript . The example also does specify /entry:main as well:

https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L82

To clarify, I had to remove part /entry:main from the example link command given in the "Hello, world" example which was

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

Added kernel32.lib legacy_stdio_definitions.lib msvcrt.lib

This is also covered in the same section, and also specified in the example build script: https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L89

Maybe they should be added to the "Hello, world" example as well?

Also, I had to run this in x64 Native Tools Command Prompt for VS 2017, Developer Command Prompt for VS2017 suggested in the tutorial didn't work.

That's very strange; what are you seeing when you try to run this in the developer command prompt? For all intents and purposes that should be calling vcvarsall.bat, which is similar to what is being done in the example build script (so that it doesn't require to be run from within a developer command prompt itself):

https://github.com/sonictk/asm_tutorial/blob/0731a1465a2fd85e1797e6a530950347f9dad341/build.bat#L34

Let me know if something in the tutorial was otherwise unclear regarding the above!

This is the output I get if I run the command that worked for me in x64 Native Tools Command Prompt for VS 2019 (I upgraded to VS 2019 since I tried this earlier, but it seems that the same problems exist in that version as before).

C:\Users\Leppaaho\Documents\Ohjelmointi\Assembler>link hello_world.obj /subsystem:console /out:hello_world_basic.exe kernel32.lib legacy_stdio_definitions.lib msvcrt.lib
Microsoft (R) Incremental Linker Version 14.29.30038.1
Copyright (C) Microsoft Corporation.  All rights reserved.

hello_world.obj : error LNK2001: unresolved external symbol ExitProcess
hello_world.obj : error LNK2001: unresolved external symbol printf
LINK : error LNK2001: unresolved external symbol mainCRTStartup
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\lib\x86\legacy_stdio_definitions.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\lib\x86\msvcrt.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
hello_world_basic.exe : fatal error LNK1120: 3 unresolved externals

Btw, when opening x64 Native Tools Command Prompt for VS 2019 it mentions that vcvarsall.bat script you mentioned

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.2
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

whereas Developer Command Prompt for VS 2019 doesn't, it only prints this part:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.2
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************

Let me know if something needs further clarification! 🙂

Hemaolle avatar Jul 30 '21 13:07 Hemaolle

Ah, I see. I think I can update the tutorial to add those instructions to the earlier part as well.

As for the developer command prompt; what's happening there is that you're getting the x86 version of the command prompt (which assumes you're developing for a 32-bit environment), to get the x64 version you want to launch the "x64 Native Tools Command Prompt for VS 2017" instead: https://docs.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line?view=msvc-160 I'll update the tutorial regarding this as well.

Thanks for the report!

sonictk avatar Aug 05 '21 05:08 sonictk