Page 1 of 1

c++ runtime

Posted: Fri Nov 21, 2003 7:16 pm
by neowert
How do you tell g++ to use a custom c++ runtime? And I assume you can tell it to statically link it?

Re:c++ runtime

Posted: Fri Nov 21, 2003 7:25 pm
by Tim
Just make sure you don't link to any of the system libraries. In my experience, if you link using gcc or g++, this will happen automatically. If you link using ld then it won't do anything automatically.

If in doubt, pass -nostdlib to ld.

Re:c++ runtime

Posted: Sat Nov 22, 2003 12:28 am
by neowert
You also cant use new or delete without a runtime. And I want those. I need to write a runtime. I know it is possible, and probably not much different from writing new and delete methods in some class. But I am not sure how to sell g++ what to use. If I dont use a runtime, I might as well use C (but I like C, so that not a problem. This is mostly curiosity)

Re:c++ runtime

Posted: Sat Nov 22, 2003 6:25 am
by Tim
If you've already got malloc and free, new and delete are easy. Just implement global new and delete operators in terms of malloc and free, and ld will use those.

Re:c++ runtime

Posted: Sat Nov 22, 2003 4:06 pm
by neowert
Ah. Thanks. I dont really use c++ (prefer C usually). I did not know you could write your own new/delete.

Re:c++ runtime

Posted: Sat Nov 22, 2003 6:54 pm
by Tim
You can do this with any C++ compiler, not just when writing your own OS.

This should do it:

Code: Select all

void *operator new(size_t bytes)
{
    return malloc(bytes);
}

void operator delete(void *ap)
{
    free(ap);
}