linker - How to link Windows universal CRT to obj file compiled with nasm -


environment: windows 10. happen using mingw version of ld linking, i'm happy use visual studio link.exe if makes things simpler.

i have following basic program in nasm:

    global  _main     extern  _printf      section .text _main:     push    message     call    _printf     add     esp, 4     ret message:     db  'hello, world', 10, 0 

and builds fine using

nasm -f win32 test.nasm 

when trying link windows crt (ucrt.lib), following error:

$ ld -o test.exe test.obj test.obj:test.nasm:(.text+0x6): undefined reference `printf' 

ok, need point linker @ ucrt library:

$ ld -o test.exe /c/program\ files\ \(x86\)/windows\  kits/10/lib/10.0.14393.0/ucrt/x86/ucrt.lib test.obj test.obj:test.nasm:(.text+0x6): undefined reference `printf' 

trying equivalent visual studio linker:

d:\code\nasm>link -out:test.exe -entry:main -subsystem:console test.obj microsoft (r) incremental linker version 14.10.25017.0 copyright (c) microsoft corporation.  rights reserved.  test.obj : error lnk2001: unresolved external symbol _printf test.exe : fatal error lnk1120: 1 unresolved externals 

this raises couple of questions:

  1. why 1 of these try find printf , other, _printf? there's underscore convention, doesn't seem understood both linkers.

  2. i used objdump -t @ symbols in ucrt.lib file. won't paste entire list, contains entries such as:

[ 4](sec 1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __imp____conio_common_vcprintf

[ 5](sec 3)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 ___conio_common_vcprintf

[ 4](sec 1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __imp____conio_common_vcprintf_p

neither printf nor _printf appears in list. mean it's not exported library? if not, library should linking?

according this ms article, ucrt.lib de-facto library c runtime , c standard library.

thanks comment michael petch, looks need manually link 1 or more libs in separate location ucrt.lib library. relevant 1 printf legacy_stdio_definitions.lib, found deep in sub-dir of vs2017 install directory, opposed ucrt.lib in windows sdk install directory.

the definition of printf provided inline in stdio.h unless macro _no_crt_stdio_inline defined, guess when building inside vs toolchain.


Comments