GCC 输出错误“对‘printf’的未定义引用”使用 NASM extern 语句访问 printf 时

发布于 2024-08-15 19:13:42 字数 1907 浏览 2 评论 0原文

我正在学习 NASM 并正在编译此代码(我在 此处< /a>)。它使用此 NASM 命令进行汇编:

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

printf.o 但此 gcc 链接命令:

gcc -o printf1  printf1.o

失败并出现错误:

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

我做错了什么?提前致谢。 (编辑:我使用的是 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\n", 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, "\n",'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 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\n", 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, "\n",'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-08-22 19:13:42

我相信您需要使 _printf 匹配 C 调用约定(就像您使用 _main 而不是 main 一样)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文