NASM Forum > Using NASM

Need help in dealing with "undefined reference to `_printf'" error

(1/2) > >>

nohemon:
This is my code:


--- Code: ---    global  _main
    extern  _printf

    section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    ret
message:
    db  'Hello, World', 10, 0
--- End code ---

I use nasm and gcc for compilation. I'm on a Windows 10 64-bit system.
I run nasm -f win64 source.asm. The .obj file is created with no errors.
Then I run gcc -m64 source.obj -o source.exe and I get these errors:

source.obj:source.asm:(.text+0x6): undefined reference to `_printf'

and

C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'

What am I missing?

Frank Kotler:
Hi nohemon,

Welcome to the forum.

I can't help you much with this. I haven't run Windows since win98.

However... what you show is 32 bit code. Assemble/link it as 32 bit or change it to 64 bit. I hope a Windows user will help you!

Best,
Frank

Mathi:
Hi nohemon,

       You are trying to compile a 32 bit example with 64 bit assembler/linker switches

If you want the 32 bit version. Your version should work , with proper assemble and linking commands.


--- Code: ---global  _main
;;assemble with > nasm -fwin32 hello32.asm
;;Link with > gcc -m32 -mconsole hello32.obj -o hello32.exe

    extern  _printf

    section .text
_main:
   
    push    message
    call    _printf
    add     esp, 4
   
    mov eax,0  ;;return code of our program
    ret
message:
    db  'Hello, World', 10, 0

--- End code ---



If you intended to do this in 64 bit :

You may need to go through the below links for more info.
1)https://cs.lmu.edu/~ray/notes/nasmtutorial/
especially the 'Using Nasm in Windows' section. (Nice tutorial . Author might be in this forum)

2) x64 calling convention for Windows.
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019


1) What I noticed is that you need to lose the underscore (main instead of _main and printf instead of _printf)  when linking with C libraries in 64 bit. (I dont' have a clear explanation for this).

2) for undefined reference to WinMain : you need to use console subsystem to tell the linker that this is a console application.
Link with : gcc -m64 -mconsole hello.obj -o hello.exe   



--- Code: ---
;;64 bit version.
;;assemble with >  nasm -fwin64 hello.asm
;; link with >  gcc -m64 -mconsole hello.obj -o hello.exe
global  main
    extern  printf

    section .text
main:
;---------
    push rbp
    mov rbp, rsp
    ;; reserve for local variables
    ;; 2 qwords for argc and argv
    ;; and 1 qword for return value
    ;; 1 more qword to align with 16 bits
    sub rsp, 8*4   

    mov rcx,   message
    call    printf
   
    mov rax,0
    add rsp,8*4    ;; deallocate local variables
    pop rbp
    ret
;---------
message:
    db  'Hello, World', 13, 10, 0

--- End code ---

Mathi:
BTW

@Frank ,
        How are you ?  :)

Frank Kotler:
Hi Mathi!

Thank you! Good to hear from you. I am old and tired, but in good health.

"main" and "printf" - without underscores - are for gnu C (Linux) too. Needed for 32 bit Windows. Watcom C wants "main_"  and "printf_" etc. Go figure!

Nasm has "--prefix" and "--postfix" so we can add 'em from the command line...

Best,
Frank

Navigation

[0] Message Index

[#] Next page

Go to full version