【发布时间】:2013-01-27 21:46:08
【问题描述】:
test.c 和 kernel.asm 位于文件夹 src,Makefile在文件夹Debug中,就像这样:
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
制作文件
# 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 $@ $<
【问题讨论】:
-
将
-lc添加到 ld 命令行可能会有所帮助。 -
除非您有自己的 printf 版本,否则您必须链接 C 运行时库 (
-lc)。 -
除了“-lc”、“-I/lib/ld-linux.so.2”之外,您可能还需要另一个 ld 标志。默认情况下,ld 会查找“...so.1”,这会给出一个令人困惑的“找不到文件”错误!
标签: c gcc assembly makefile nasm