0

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) $<
  • 2
    And if you link with `gcc` instead of `ld`? – M Oehm Jan 03 '15 at 16:15
  • Have you tried adding -lc to LDFLAGS? – Kris Jan 03 '15 at 16:18
  • 1
    http://stackoverflow.com/questions/1214365/disable-warning-messages-in-gcc-through-header-files – Biparite Jan 03 '15 at 16:19
  • @MOehm AFAIK, since I have also assembly files to be compiled and linked I have to use ld for linking –  Jan 03 '15 at 16:24
  • yes @Kris I tried but i gives - ld: cannot find -lc –  Jan 03 '15 at 16:25
  • use cmake to generate the make file you will have less boilerplate code to maintain, by the way, try: `-static-libgcc` or more portable `-shared-libgcc` – CoffeDeveloper Jan 03 '15 at 16:25
  • Why don't you use fgets() ? – SVN Jan 03 '15 at 16:28
  • What environment are you building on? (btw, I assume that your problem has nothing to do with using gets vs fgets or whatever. You probably can't use any of the functions from the standard library, correct?) – Kris Jan 03 '15 at 16:31
  • I changed the gets() to fgets(), same result. I cannot access any function from the std library. GCC recognizes the function but the linker cannot find it. @DarioOO I tried your suggestion but also got same result. I'm building on Ubuntu 14.04 64-bit –  Jan 03 '15 at 16:38
  • If i rember correctly you were building a kernel. My guess: fgets need to interact with operative system, but if you are creting it, there's no OS to interact with? – CoffeDeveloper Jan 03 '15 at 16:45
  • 1
    @DarioOO yes it seems like. I was trying to skip mapping all the key interrupts but apparently I have to do. Thank you all by the way. –  Jan 03 '15 at 16:52
  • Never tried writing one myself so good luck :). – CoffeDeveloper Jan 03 '15 at 16:54

1 Answers1

1

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