对“printf”的未定义引用

Sea*_*ene 5 c assembly gcc makefile nasm

test.ckernel.asm位于文件夹src中, Makefile位于文件夹Debug中,就像这样:

src
    test.c
    kernel.asm
Debug
    Makefile
Run Code Online (Sandbox Code Playgroud)

所有这些文件都是非常简单的代码。但如果我make在该文件夹中运行Debug,我会收到以下错误:

ld -Ttext 0x30400 -o test ../Debug/kernel.o ../Debug/test.o
../Debug/test.o: In function `Print_String':
test.c:(.text+0xf): undefined reference to `printf'
Run Code Online (Sandbox Code Playgroud)

有人能告诉我为什么吗?内容如下:

测试.c

#include <stdio.h>

int m = 1;
void Print_String()
{
    printf("TEST");
}
Run Code Online (Sandbox Code Playgroud)

内核.asm

extern m
extern Print_String

[section .text]
global _start
_start:
    mov eax, m
    call Print_String
Run Code Online (Sandbox Code Playgroud)

生成文件

# This Program
TARGET  = test

# All Phony Targets
.PHONY : everything clean all

DIR     = ..

OBJ     = $(DIR)/Debug/kernel.o $(DIR)/Debug/test.o
C_FILE  = $(DIR)/src/test.c
K_ASM   = $(DIR)/src/kernel.asm

ASM     = nasm
LD      = ld
CC      = gcc

CC_FLAG  = -c -g
ASM_FLAG = -f elf
LD_FLAG  = -Ttext 0x30400

# Default starting position
everything : $(TARGET)

clean :
    rm -f $(TARGET)

all : clean everything

kernel.o : $(K_ASM)
    $(ASM) $(ASM_FLAG) -o $@ $<

test     : $(OBJ)
    $(LD) $(LD_FLAG) -o $@ $(OBJ)

test.o   : $(C_FILE)
    $(CC) $(CC_FLAG) -o $@ $<
Run Code Online (Sandbox Code Playgroud)

Kev*_*nko 7

除非您有自己的版本printf,否则您必须链接到 C 运行时库。

将选项传递 -lcld命令。