【问题标题】:undefined reference to `printf'对“printf”的未定义引用
【发布时间】:2013-01-27 21:46:08
【问题描述】:

test.ckernel.asm 位于文件夹 srcMakefile在文件夹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


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 2015-07-12
  • 1970-01-01
  • 2011-03-27
  • 2022-01-22
  • 2016-04-04
  • 2015-11-03
相关资源
最近更新 更多