2

I am trying to write a program, which adds 2 numbers, but I have problem with linking. The code compiles well, but I did not find any .lib file for the printf. As I know, some previuos MSVS had it in msvcrt.lib, but where is it in MSVS 2019? Code:

.686
.model flat, c
.data
format_str db "%d", 0Dh, 0Ah, 0

.code

printf proto c :dword, :vararg

AddFun:
    push ebp
    mov ebp, esp
    sub esp, 8

    mov eax, [ebp + 8]
    add eax, [ebp + 12]

    mov esp, ebp
    pop ebp
    ret   

main proc

    push 1
    push 2
    call AddFun
    add esp, 8

    invoke printf, offset format_str, eax

    ret
main endp

end

Compile with

ml /c main.asm

Result

Microsoft (R) Macro Assembler Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Assembling: main.asm

Link with

link main.obj /entry:main /subsystem:console

gives me

Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

main.obj : error LNK2019: unresolved external symbol _printf referenced in function _main

So,

link main.obj /entry:main /subsystem:console msvcrt.lib

also does not work

I tried to link with legacy_stdio_definitions.lib as some answer in the possible duplicates suggests, but it gave me a lot of new unresolved external symbols

___acrt_iob_func referenced in function __vwprintf_l
___stdio_common_vfwprintf referenced in function __vfwprintf_l
...
Ivan
  • 316
  • 3
  • 15
  • Use these include lib directives in your asm source file: | includelib msvcrtd | includelib oldnames | includelib legacy_stdio_definitions.lib | and in the .code section, the needed externs: | extern printf:near | . The includelib directives should cause VS to link the appropriate library files. – rcgldr Sep 21 '19 at 20:38
  • @rcgldr sorry for not asnwering that long, i lost my hope after my question was marked as a duplicate. And I want to thank you a lot! Because your method seems working for me. I added all libs you wrote, and it finally compiled, but the extern printf:near did a redefinition of printf, so it works without this line. Thanks for answering! – Ivan Sep 22 '19 at 11:43
  • If using standard calling convention, you don't need the proto. I haven't tried fastcall version of printf to see if that needs a proto. In 64 bit mode, there's only fastcall, and extern printf:near works. – rcgldr Sep 22 '19 at 13:59

0 Answers0