Having trouble with header fiels
Posted: Sat May 22, 2010 8:18 pm
This is what is happening: I call a function (any function) and the CPU faults and reboots (on Bochs). Before I had everything in the same file, but decided to move into headers. I have one header file for every c file meaning screen.h/screen.c. All functions create a fault so I know it's not the code inside the function bodies (also have used for loops). Now for example inside screen.h I have a function declaration as: extern void cls(); (surrounded by #ifndef/#define/#endif). All header files are inside an include folder so I add the option: -I./include/kernel when compiling. Now in screen.c, I first include screen.h and then add my function code, like void cls() {...}. In kmain.c I include the screen.h file and I call the function cls. It faults and reboots.
Here is the code if you didn't understand what I'm saying (These are only the relevant parts of the files):
kmain.c:
screen.h:
screen.c:
Please help me to see what I'm doing wrong. Also here's how I'm compiling: (I'm using a Makefile)
I then move floppy.img into the bochs directory and start bochs.
linker.ld
Thanks for taking the time to look at this.
Here is the code if you didn't understand what I'm saying (These are only the relevant parts of the files):
kmain.c:
Code: Select all
#include <../include/kernel/screen.h>
void kmain(void* mbd, unsigned int magic)
{
cls();
....
}
Code: Select all
#ifndef SCREEN_H
#define SCREEN_H
#include <../include/kernel/common.h>
extern void cls();
#endif
Code: Select all
#include <../include/kernel/screen.h>
uint16_t *video = (uint16_t *) 0xB8000;
uint8_t xpos = 0;
uint8_t ypos = 0;
void cls()
{
uint8_t aByte = (0 << 4) | (15 & 0x0F);
uint16_t blank = 0x20 | (aByte << 8);
uint32_t i;
for (i = 0; i < 80*25; i++)
{
video[i] = blank;
}
xpos = 0;
ypos = 0;
}
Code: Select all
CSOURCES=kernel.c screen.c
SOURCES=loader.o kernel.o screen.o
CFLAGS=-nostdlib -nostdinc -fno-builtin -I../include/
LDFLAGS=-Tlinker.ld
all:
nasm -f elf -o loader.o loader.s
gcc -o kernel.o -c kernel.c $(CFLAGS)
gcc -o screen.o -c screen.c $(CFLAGS)
i586-elf-ld $(LDFLAGS) -o kernel.bin $(SOURCES)
cat kpad kernel.bin > floppy.img
linker.ld
Code: Select all
ENTRY (loader)
SECTIONS{
. = 0x00100000;
.text :{
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata)
}
.data ALIGN (0x1000) : {
*(.data)
}
.bss : {
sbss = .;
*(COMMON)
*(.bss)
ebss = .;
}
}