C++ kernel

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
crazybuddha

C++ kernel

Post by crazybuddha »

Although this forum is typically about boot loaders, I thought I'd ask if anyone has ventured into writing their kernel in C++ (not C)??

I can get plenty of nay-saying (practical or otherwise) at alt.os.development. I'm wondering about actual coding attempts.
The_Legend

Re:C++ kernel

Post by The_Legend »

I tried it.

There are two problems, the second may be a bug.

The first problem is that the compiler needs some built-in functions for some c++-features, more than the C-ones like _ftol, this will be a real problem when you use RTTI and Exceptions.

There are two solutions: Either reprogram those functions (hard work), or, for example, gcc has a library containing those functions, but this library requires other functions, which are of course easier to write, but still not really easy.


I had a problem with global vars. I had two instances created as global vars:

PhysRAM prRAM;
GDT gdtGDT;

and only the constructor of prRAM was called for some reason.

Solution: Is this a compiler bug which is removed in a newer version?
A cleaner solution would be to use singletons.
crazybuddha

Re:C++ kernel

Post by crazybuddha »

Legend-
Did you abandon it in favor of C??


For anyone interested in OS in C++, here is a friendly tour through a version of NachOS:

http://www-ali.cs.umass.edu/salsa/topics.html
The_Legend

Re:C++ kernel

Post by The_Legend »

No, I just stopped my OS until I could fix that, as C++ would be more natural for an OS then C is. I'll try again soon ...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C++ kernel

Post by Pype.Clicker »

The_Legend wrote: I tried it.

There are two solutions: Either reprogram those functions (hard work), or, for example, gcc has a library containing those functions, but this library requires other functions, which are of course easier to write, but still not really easy.
The first approach (reprogramming the exceptions or RTTI runtime) is not feasible, as their behaviour and interface may change from compiler to compiler (unlike say, malloc and main API which are the same for every C compiler).

as i show it here, even the same compiler (gcc) changed the way processing exceptions from one version to another :(

Thus you can either disable them (-fno-exception ...) or link your kernel statically to libgcc++.a so that you have the runtime support you need. Once this is done, you still need to reimplement what the runtime requires like malloc, etc.
I had a problem with global vars. I had two instances created as global vars:

PhysRAM prRAM;
GDT gdtGDT;

and only the constructor of prRAM was called for some reason.
Very strange behaviour ... did you reproduced the bug with some other code using the same compiler or with same code on some other compiler.
The_Legend

Re:C++ kernel

Post by The_Legend »

I wanted to try gcc 3.03, but when I compiled with this compiler, it suddenly wanted another function, for which I couldn't guess the use of it. Just like what you have explained.
anubis

Re:C++ kernel

Post by anubis »

Hi
I almost thought to write an OS kernel in c++ for my term project that could boot independently from a bootloader ;-) .
Is there any way around to avoid the problems for writing a kernel in c++. ???

If that is the case i will have to write code in c that emulates c++ object oriented approach . Thats a big project in itself. :( :'(
The_Legend

Re:C++ kernel

Post by The_Legend »

First, just like Pype.Clicker said, either disable special features of C++ like RTTI and Exceptions, leaving virtual functions, operator overloading etc. in it (I hope ...) or
link with that lib and implement all functions needed by it.

Second, Singletons are much cleaner then global vars. :)
FSQUID

Re:C++ kernel

Post by FSQUID »

TC++ with a specail self-written start-module works ok. Except the vararg-functions like cprintf, sprintf, usw...
Post Reply