Page 1 of 1

moving kernel from C to C++

Posted: Sat Jul 26, 2008 5:05 pm
by marun
At first "Hello world!" to everybody since it is my first post on this forum.

I'm using James Molloy boot.s (http://www.jamesmolloy.co.uk/tutorial_h ... nesis.html) with one exception I changed "main" to "kmain".

My kmain.c is:

Code: Select all

struct multiboot;

int kmain(struct multiboot *mboot_ptr){
  return 0xDEAD0000;
}
My kmain.cpp is:

Code: Select all

struct multiboot;

extern "C" int kmain(struct multiboot *mboot_ptr){
  return 0xDEAD0000;
}
Output of "nm kmain.o" for both files gives:
00000000 T kmain
Linking with c version goes perfectly fine, but when I try to link with c++ version linker reports:
boot.o: In function `start':
boot.s:(.text+0x1f): undefined reference to `kmain'
options passed to gcc:
CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector -Wall -c

options passed to linker:
LDFLAGS=-Tlink.ld

options passed to nasm:
ASFLAGS=-felf

options passed to g++:
CPPFLAGS=-nostartfiles -nostdlib -nostdinc -fno-builtin -fno-stack-protector -Wall -c

I'm using linux as development platform. Does anybody know why I can't link against kmain.o file generated by g++?

Re: moving kernel from C to C++

Posted: Sat Jul 26, 2008 7:15 pm
by niteice

Code: Select all

extern "C"

Re: moving kernel from C to C++

Posted: Sat Jul 26, 2008 7:49 pm
by marun
Problem solved...
Code is perfectly fine. The problem was some compiler problem (recompilation of compiler does the trick).

@niteice: extern "C" is needed. Else "kmain" will be (on g++) mangled to "_Z5kmainP9multiboot"

Thanks to everyone.

Re: moving kernel from C to C++

Posted: Sun Jul 27, 2008 2:57 am
by JamesM
Hi,
options passed to g++:
CPPFLAGS=-nostartfiles -nostdlib -nostdinc -fno-builtin -fno-stack-protector -Wall -c
That should be CXXFLAGS. CPP is the C Preprocessor (/usr/bin/cpp).

Cheers,

James

Re: moving kernel from C to C++

Posted: Sun Jul 27, 2008 6:05 am
by marun
You are right. It's just time (1 am) when I was writing my Makefile... heh...

Re: moving kernel from C to C++

Posted: Sun Jul 27, 2008 10:20 am
by AJ
Hi,

If you are an anal retentive like me, you may like to look at Solar's Makefile tutorial, for all the other warning flags you should be using :)

Cheers,
Adam