2013-02-12 68 views
4

test.c的kernel.asm在文件夹中SRC生成文件是文件夹调试中,只是像这样:未定义参考`的printf”

src 
    test.c 
    kernel.asm 
Debug 
    Makefile 

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

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' 

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

test.c的

#include <stdio.h> 

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

kernel.asm

extern m 
extern Print_String 

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

的Makefile

# 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 [email protected] $< 

test  : $(OBJ) 
    $(LD) $(LD_FLAG) -o [email protected] $(OBJ) 

test.o : $(C_FILE) 
    $(CC) $(CC_FLAG) -o [email protected] $< 
+3

将'-lc'添加到ld命令行可能会有所帮助。 – 2013-02-12 14:42:08

+10

除非你有自己的printf版本,否则你必须链接到C运行库('-lc')。 – 2013-02-12 14:42:40

+2

除“-lc”,“-I/lib/ld-linux.so.2”外,您可能还需要另一个ld标志。 ld,默认情况下,查找“... so.1”,这会给出一个令人困惑的“文件未找到”错误! – 2013-02-12 23:30:06

回答

3

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

将选项-lc传递给ld命令。