简体   繁体   English

GCC链接器不链接标准库

[英]GCC linker does not link standard library

I'm developing a basic kernel for my term project. 我正在为我的学期项目开发一个基本内核。 Until now, I haven't used any standard libraries in my project but I needed gets() , I included <stdio.h> . 到目前为止,我在项目中尚未使用任何标准库,但我需要gets() ,其中包含了<stdio.h> GCC finds the header location but the linker gives error : GCC找到标头位置,但链接器给出错误:

ld -melf_i386 -Tlink.ld  -o kernel boot.o main.o monitor.o common.o descriptor_tables.o isr.o interrupt.o gdt.o timer.o
main.o: In function `main':
main.c:(.text+0x53): undefined reference to `gets'

This is my Makefile file, 这是我的Makefile文件,

SOURCES=boot.o main.o monitor.o common.o descriptor_tables.o isr.o interrupt.o gdt.o timer.o

CFLAGS= -m32 -fno-stack-protector -fstack-check 
LDFLAGS= -melf_i386 -Tlink.ld
ASFLAGS=-felf

all: $(SOURCES) link

clean:
    -rm *.o kernel

link:
    ld $(LDFLAGS) -o kernel $(SOURCES)

.s.o:
    nasm $(ASFLAGS) $<

You cannot use the C library for a kernel as it is build for an existing kernel and relies on the syscalls of its target OS. 您不能将C库用于内核,因为它是为现有内核构建的并且依赖于其目标OS的syscall。 Instead, you have to write a driver for keyboards and everything else you need to get characters from anywhere. 相反,您必须编写键盘驱动程序,以及从任何地方获取字符所需的所有其他操作。 getc() is a very advanced function from that point of view and you should consider making the basic functions of the kernel stable before programming anything to interact with. 从这个角度来看, getc()是一个非常高级的函数,在编写任何要与之交互的内容之前,您应该考虑使内核的基本功能稳定。

By the way, you should really build a cross compiler . 顺便说一句,您应该真正构建一个交叉编译器 It has many advantages over feeding the system compiler with awkward options. 与为系统编译器提供笨拙的选项相比,它具有许多优点。 After all, the kernel is meant to run on different machines, so it should be compiled for bare x86, which is what a cross-compiler does. 毕竟,内核是要在不同的机器上运行的,因此应针对裸x86进行编译,这是交叉编译器的工作。

Keep coding this thing! 继续编码这个东西! leitimmel leitimmel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM