简体   繁体   中英

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> . GCC finds the header location but the linker gives error :

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,

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. 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.

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.

Keep coding this thing! leitimmel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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