Undefined reference to kmain despite giving reference? ( C )

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
JamieEdwards
Posts: 11
Joined: Fri Apr 06, 2012 5:18 am

Undefined reference to kmain despite giving reference? ( C )

Post 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
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: Undefined reference to kmain despite giving reference? (

Post 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...
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
JamieEdwards
Posts: 11
Joined: Fri Apr 06, 2012 5:18 am

Re: Undefined reference to kmain despite giving reference? (

Post 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.
zeusk
Posts: 16
Joined: Fri Sep 14, 2012 1:09 pm

Re: Undefined reference to kmain despite giving reference? (

Post by zeusk »

Have you tried marking the kmain function as global ???

Code: Select all

__attribute__((externally_visible))
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

Re: Undefined reference to kmain despite giving reference? (

Post by dansmahajan »

use ENTRY(_kmain) in your link.ld file
JamieEdwards
Posts: 11
Joined: Fri Apr 06, 2012 5:18 am

Re: Undefined reference to kmain despite giving reference? (

Post by JamieEdwards »

The CPP_ reference in the makefile was the cause of this and correcting it stopped the error XD Thank's anyway :D
Post Reply