English 中文(简体)
GCC outputs error "undefined reference to `printf " when using an NASM extern statement to access printf
原标题:

I am learning NASM and am tying to compile this code (which I found here). It assembles using this NASM command:

nasm -f coff -l printf.lst  printf1.asm

to printf.o but this gcc linking command:

gcc -o printf1  printf1.o

fails with the error:

printf1.o:printf1.asm:(.text+0x1a): undefined reference to `printf 
collect2: ld returned 1 exit status

What am I doing wrong? Thanks in advance. (EDIT: I m on Windows 7);

; printf1.asm   print an integer from storage and from a register
; Assemble: nasm -f coff -l printf.lst  printf1.asm
; Link:     gcc -o printf1  printf1.o
; Run:      printf1
; Output:   a=5, eax=7

; Equivalent C code
; /* printf1.c  print an int and an expression */
; #include 
; int main()
; {
;   int a=5;
;   printf("a=%d, eax=%d
", a, a+2);
;   return 0;
; }

; Declare some external functions
;
        extern  printf      ; the C function, to be called

section .data           ; Data section, initialized variables

        a: dd   5           ; int a=5;
        fmt: db "a=%d, eax=%d", 10, 0 ; The printf format, "
", 0 


section .text               ; Code section.

        global _main        ; the standard gcc entry point
_main:              ; the program label for the entry point
        push    ebp     ; set up stack frame
        mov     ebp,esp

    mov eax, [a]    ; put a from store into register
    add eax, 2      ; a+2
    push    eax     ; value of a+2
        push    dword [a]   ; value of variable a
        push    dword fmt   ; address of ctrl string
        call    printf      ; Call C function
        add     esp, 12     ; pop stack 3 push times 4 bytes

        mov     esp, ebp    ; takedown stack frame
        pop     ebp     ; same as "leave" op

    mov eax,0       ;  normal, no error, return value
    ret         ; return
最佳回答

I believe you need to make that _printf to match the C calling convention (just like you used _main instead of main).

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签