I have basic hobby kernels for IA32 and AMD64 which are written in C++ with assembler used where sensible / required. Take a look at the wiki page http://wiki.osdev.org/C%2B%2B
, which has lots of information about supporting the C++ runtime features (and links to creating global constructors).
Essentially, if you consider C++ as "C with classes", there's very little work to do over what you would need anyway for a C kernel.
jack.chaney wrote:The functions new and delete require malloc and free to be implemented
If you write a kernel in C, you're going to be needing a malloc(n) (or kmalloc(n)) function anyway. In C++, adding the code:
Code: Select all
void* operator new ( size_t count )
// in my kernel, the heap is managed by a class and I call HeapManager::Allocate(count);
basically gets you a working 'new' operator. The same applies to delete / free and the array versions of these functions. You obviously have to make sure that new is not called before your heap manager is online, but this also applies to C and malloc.
The other big
C++ feature that you will want is global and static classes. These can be added by calling GCC's own init functions (provided by the freestanding cross compiler). If you want RTTI or exceptions (should you use these in a kernel?), then things get a little more complex, but still not too bad. TBH, I've coped very nicely without either of these features anyway.
jack.chaney wrote:and stream requires stdin and stdout to be active
Yes, but this also applies to C. I have a "DebugConsole" class. For each architecture, a default debug device is added and you can very quickly have something like:
Code: Select all
debugConsole << "This is a debug message. Installed Memory: " << memCount << " bytes.";
Again, you would need some sort of debug output in C anyway, and writing C++-style versions is simply a case of wrapping. Later, when your device manager and VFS are online, you can redirect the early "DebugConsole" to stderr, or wherever you want to point it. In my core kernel, I make strong use of the singleton and factory patterns to aid all of this.
If you are more familiar with C++ in application development, I would strongly encourage anyone to give it a try in system programming land. Not for everyone, but I found it pretty effective.