Page 1 of 1

Undefined reference to kmain despite giving reference? ( C )

Posted: Wed Jan 30, 2013 4:37 pm
by JamieEdwards
Hi,

I'm getting a very frustrating problem with my linker, I try compiling and linking my very basic kernel and I get the following error from my linker:

Code: Select all

S$ make
ld -Tlink.ld -melf_i386  -o kern/kernel obj/boot.o
obj/boot.o: In function `start':
core/arch/boot.asm:(.__mbHeader+0x24): undefined reference to `kmain'
make: *** [kern/kernel] Error 1
The really stupid thing is that my previous O.S project was fine using the same code as below and it compiled fine, and booted fine.

I've also tried using the "_kmain" and then using the make parameter "-fno-leading-underscore" with the same outcome. Any idea's?

main.c:

Code: Select all

#include <system.h>


void kmain( struct multiboot *mbd, unsigned int magic )
{
    return 0xDEADBEEF;
}
boot.asm:

Code: Select all

; boot.asm -- The bootstrap for GRUB

; The Multiboot macroes for telling GRUB how to load the kernel

section .__mbHeader

MBOOT_PAGE_ALIGN        equ         1<<0                        ; Load everything on a page boundary
MBOOT_MEM_INFO          equ         1<<1                        ; Provide the kernel with the memory map
MBOOT_HEADER_MAGIC      equ         0x1BADB002                  ; Multiboot magic number, required by GRUB

MBOOT_HEADER_FLAGS      equ         MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
MBOOT_CHECKSUM          equ         -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)


[bits 32]

[global mboot]
[extern code]
[extern bss]
[extern end]

mboot:
    dd  MBOOT_HEADER_MAGIC
    dd  MBOOT_HEADER_FLAGS
    dd  MBOOT_CHECKSUM

    dd  mboot
    dd  code
    dd  bss
    dd  end
    dd  start

[global start]
[extern kmain]

start:
    push    esp
    push    ebx

    cli
    call    kmain
    jmp     $
makefile:

Code: Select all

CPP_MAIN_SOURCE = main.c
CPP_ARCH_SOURCES =
CPP_CORE_SOURCES =

# Make sure to add the likes of the directories arch and gui etc here too!

ASM_SOURCES = boot.asm

# Here are the object files for the sources above
C_MAIN_OBJECTS = $(patsubst %.c,obj/%.o,$(C_MAIN_SOURCE))
C_ARCH_OBJECTS = $(patsubst %.c,obj/%.o,$(C_ARCH_SOURCES))
C_CORE_OBJECTS = $(patsubst %.c,obj/%.o,$(C_CORE_SOURCES))

# Make sure to add the object files for directories like "gui" etc...

ASM_OBJECTS = $(patsubst %.asm,obj/%.o,$(ASM_SOURCES))

# Flags for the C/CPP and ASM compilers, and also for the Linker
CFLAGS = -c \
    -nostdlib \
    -nostdinc \
    -fno-builtin \
    -fno-stack-protector \
    -fno-leading-underscores \
    -m32 \
    -Iinclude \
    -Wall \
    -Wextra

LDFLAGS = -Tlink.ld \
    -melf_i386 \
 #   --oformat = elf32-i386
ASFLAGS = -felf

# Compilers themselves
CC = gcc
ASC = nasm

# Now onto the compiling
all: kern/kernel

.PHONY: clean

clean:
	-rm -f obj/*.o, kern/kernel

# NB: C_GUI_OBJECTS ( if its going to be called that ) will need to go in here too!
kern/kernel: $(C_MAIN_OBJECTS) $(C_ARCH_OBJECTS) $(C_CORE_OBJECTS) $(ASM_OBJECTS)
	ld $(LDFLAGS) -o $@ $^
	@echo "Linking Everything for kernel..."
	@echo "Linking Complete..."
	@echo "\n\n\nKernel is ready for use"

$(C_MAIN_OBJECTS): obj/%.o : core/%.c
	$(CC) $(CFLAGS) $< -o $@
	@echo "Completed main.c in \"core\""

vpath %.c core

$(C_ARCH_OBJECTS): obj/%.o : core/arch/%.c
	$(CC) $(CFLAGS) $< -o $@
	@echo "Completed all .c files in \"core/arch\""

vpath %.c core/arch

$(C_CORE_OBJECTS): obj/%.o : core/%.c
	$(CC) $(CFLAGS) $< -o $@
	@echo "Completed all .c files in \"core\""

vpath %.c core

$(ASM_OBJECTS): obj/%.o : core/arch/%.asm
	$(ASC) $(ASFLAGS) $< -o $@
	@echo "Completed all .asm files"

vpath %.asm core/arch

Re: Undefined reference to kmain despite giving reference? (

Posted: Wed Jan 30, 2013 4:39 pm
by Griwes
Check the names of three variables at the top of Makefile, that should make it quite clear. Also, I am not quite sure how did you get to mixing "C_" and "CPP_" Makefile variables in one Makefile...

Re: Undefined reference to kmain despite giving reference? (

Posted: Wed Jan 30, 2013 5:35 pm
by JamieEdwards
Griwes wrote:Check the names of three variables at the top of Makefile, that should make it quite clear. Also, I am not quite sure how did you get to mixing "C_" and "CPP_" Makefile variables in one Makefile...
How did I not see that? XD Thank you, and in answer to your question, I upgraded my O.S but it failed and I lost everything I did in my previous project, luckily I had an old backup I'd saved to my server with and opened that up to find it wasn't up to date.

I thought bugger it, I'll reuse the old makefile and start a C++ kernel instead ( which failed completely ) so I changed back to C and tried to update my makefile as best as possible.

Re: Undefined reference to kmain despite giving reference? (

Posted: Wed Jan 30, 2013 5:39 pm
by zeusk
Have you tried marking the kmain function as global ???

Code: Select all

__attribute__((externally_visible))

Re: Undefined reference to kmain despite giving reference? (

Posted: Wed Feb 06, 2013 11:27 am
by dansmahajan
use ENTRY(_kmain) in your link.ld file

Re: Undefined reference to kmain despite giving reference? (

Posted: Wed Feb 06, 2013 12:16 pm
by JamieEdwards
The CPP_ reference in the makefile was the cause of this and correcting it stopped the error XD Thank's anyway :D