Issues linking JamesM tutorial

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sdfkjsdfkj
Posts: 15
Joined: Thu Jan 20, 2011 4:19 pm

Issues linking JamesM tutorial

Post by sdfkjsdfkj »

Okay.. looks like I'm back here to ask stupid questions. My project so far is a mix of JamesM's tutorials and Bran's tutorials, after adding the heap code and fixing typos I'm getting linker errors saying that 'kernel_directory' and 'current_directory' have been defined multiple times.If I don't include "paging.h" from my main file one set of the messages disappears, but I've got ifdef guards and I don't understand why I'm getting them at all. Looking into "kheap.c" and "paging.c" they each have an include for the others header file, and I'm wondering if this could be part of the issue. Both 'kernel_directory' and 'current_directory' are declared in "paging.h". I'm almost positive I'm overlooking something stupid, help would be much appreciated.
Linker messages:
ld -Tlink.ld -o kernel boot.o main.o common.o screen.o gdt.o idt.o isrs.o irq.o timer.o kb.o kheap.o paging.o ordered_array.o
kheap.o:(.bss+0x0): multiple definition of `kernel_directory'
main.o:(.bss+0x0): first defined here
kheap.o:(.bss+0x4): multiple definition of `current_directory'
main.o:(.bss+0x4): first defined here
paging.o:(.bss+0x0): multiple definition of `kernel_directory'
main.o:(.bss+0x0): first defined here
paging.o:(.bss+0x4): multiple definition of `current_directory'
main.o:(.bss+0x4): first defined here
make: *** [link] Error 1
paging.h

Code: Select all

// paging.h -- Defines the interface for and structures relating to paging.
//             Written for JamesM's kernel development tutorials.

#ifndef PAGING_H
#define PAGING_H

#include <system.h>
#include <common.h>

//...

// The kernel's page directory
page_directory_t *kernel_directory=0;

// The current page directory;
page_directory_t *current_directory=0;

//...
#endif
paging.c

Code: Select all

// paging.c -- Defines the interface for and structures relating to paging.
//             Written for JamesM's kernel development tutorials.

#include "paging.h"
#include "kheap.h"

// A bitset of frames - used or free.
u32int *frames;
u32int nframes;

// Defined in kheap.c
extern u32int placement_address;
extern heap_t* kheap;

//...
kheap.h

Code: Select all

// kheap.h -- Interface for kernel heap functions, also provides
//            a placement malloc() for use before the heap is 
//            initialised.
//            Written for JamesM's kernel development tutorials.

#ifndef KHEAP_H
#define KHEAP_H

#include "common.h"
#include "ordered_array.h"

#define KHEAP_START         0xC0000000
#define KHEAP_INITIAL_SIZE  0x100000

#define HEAP_INDEX_SIZE   0x20000
#define HEAP_MAGIC        0x123890AB
#define HEAP_MIN_SIZE     0x70000

//declarations ...

#endif // KHEAP_H
kheap.c

Code: Select all

// kheap.c -- Kernel heap functions, also provides
//            a placement malloc() for use before the heap is 
//            initialised.
//            Written for JamesM's kernel development tutorials.

#include "kheap.h"
#include "paging.h"

// end is defined in the linker script.
extern u32int end;
u32int placement_address = (u32int)&end;
extern page_directory_t *kernel_directory;
heap_t *kheap=0;

//implementations...
Last edited by sdfkjsdfkj on Sat Mar 24, 2012 8:32 pm, edited 1 time in total.
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: Issues linking JamesM tutorial

Post by brain »

Can you trim it down a bit to where you think the issue may be? Imho this is too much to expect people to read, what have you already tried yourself in debugging and troubleshooting?
sdfkjsdfkj
Posts: 15
Joined: Thu Jan 20, 2011 4:19 pm

Re: Issues linking JamesM tutorial

Post by sdfkjsdfkj »

*facepalm* Sorry about that, somehow I managed to forget how large the files actually were. So far I've removed the include from main (which removed one of the sets of messages), moved the include paging.h from kheap.c into kheap,h (nothing changed), mover kernel_directory and current_directory into paging.c out of the header (nothing changed), and moved the include for kheap.h inside paging.c into paging.h (nothing changed). I considered removing the include for kheap.h inside paging.c, and externing all the required things from kheap but figured that kinda makes the header file useless if I have to do that everywhere I need kheap stuff.
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Issues linking JamesM tutorial

Post by Rudster816 »

You're defining the variable inside of the header when you just want to have a forward declaration. Add the extern keyword inside of the header to tell the compiler that the variable will be resolved by the linker at link time, and also define it in a .c file.
sdfkjsdfkj
Posts: 15
Joined: Thu Jan 20, 2011 4:19 pm

Re: Issues linking JamesM tutorial

Post by sdfkjsdfkj »

When I extern it in the header:

Code: Select all

extern page_directory_t* kernel_directory;
extern page_directory_t* current_directory;
and set the value in the c file (left unchanged from last time):

Code: Select all

// The kernel's page directory
page_directory_t* kernel_directory = 0;
// The current page directory;
page_directory_t* current_directory = 0;
I still get multiple definition linker errors
Snake
Posts: 19
Joined: Wed Mar 21, 2012 7:38 pm

Re: Issues linking JamesM tutorial

Post by Snake »

Whats in the main.c? Does that also define kernel_directory? Because that's where it is defined first according to the error message.

I bet that this is because main.c includes paging.h and you did not do a make clean to get a full rebuild.
sdfkjsdfkj
Posts: 15
Joined: Thu Jan 20, 2011 4:19 pm

Re: Issues linking JamesM tutorial

Post by sdfkjsdfkj »

Yeah main.c includes paging.h, doing the full rebuild appears to have worked, thanks everyone!
Post Reply