moving kernel from C to 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
marun
Posts: 6
Joined: Sat Jul 26, 2008 4:30 pm

moving kernel from C to C++

Post 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++?
niteice
Member
Member
Posts: 59
Joined: Tue Oct 03, 2006 3:49 pm

Re: moving kernel from C to C++

Post by niteice »

Code: Select all

extern "C"
marun
Posts: 6
Joined: Sat Jul 26, 2008 4:30 pm

Re: moving kernel from C to C++

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: moving kernel from C to C++

Post 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
marun
Posts: 6
Joined: Sat Jul 26, 2008 4:30 pm

Re: moving kernel from C to C++

Post by marun »

You are right. It's just time (1 am) when I was writing my Makefile... heh...
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: moving kernel from C to C++

Post 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
Post Reply