Skip to content

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

Open

Description

@Hemaolle

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.

Activity

  1. self-assigned this
    on Jul 27, 2021
  2. sonictk commented on Jul 27, 2021

    @sonictk
    Owner

    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:

    set BinLinkerFlagsMSVC=/subsystem:console /entry:main

    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:

    set CommonLinkerFlagsMSVC=%BinLinkerFlagsMSVC% /defaultlib:ucrt.lib /defaultlib:msvcrt.lib /defaultlib:legacy_stdio_definitions.lib /defaultlib:Kernel32.lib /defaultlib:Shell32.lib /nologo /incremental:no

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

    call "%vs2017installdir%\VC\Auxiliary\Build\vcvarsall.bat" x64

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

  3. added
    question
    on Jul 27, 2021
  4. Hemaolle commented on Jul 30, 2021

    @Hemaolle
    Author

    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:

    set BinLinkerFlagsMSVC=/subsystem:console /entry:main

    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:

    set CommonLinkerFlagsMSVC=%BinLinkerFlagsMSVC% /defaultlib:ucrt.lib /defaultlib:msvcrt.lib /defaultlib:legacy_stdio_definitions.lib /defaultlib:Kernel32.lib /defaultlib:Shell32.lib /nologo /incremental:no

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

    call "%vs2017installdir%\VC\Auxiliary\Build\vcvarsall.bat" x64

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

  5. sonictk commented on Aug 5, 2021

    @sonictk
    Owner

    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!

  6. hasnogaems commented on Dec 17, 2024

    @hasnogaems

    Same issue here

    Microsoft (R) Incremental Linker Version 14.42.34435.0
    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
    hello_world_basic.exe : fatal error LNK1120: 2 unresolved externals
    

    Will try solutions from thread

  7. pm100 commented on Mar 19, 2025

    @pm100

    Yup the tutorial still has a bare link command (and no call to CRT_INIT

    Had to come here and pull the linker flags from the build.bat script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions