5

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

src
    test.c
    kernel.asm
Debug
    Makefile

所有这些文件都是非常简单的代码。但是如果我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'

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

测试.c

#include <stdio.h>

int m = 1;
void Print_String()
{
    printf("TEST");
}

内核.asm

extern m
extern Print_String

[section .text]
global _start
_start:
    mov eax, m
    call Print_String

生成文件

# 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 $@ $<
4

1 回答 1

4

除非您在某个地方拥有自己的版本,否则您必须printf与 C 运行时库链接。

将选项传递 -lcld命令。

于 2014-05-14T01:39:32.237 回答