c++ runtime

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
neowert

c++ runtime

Post by neowert »

How do you tell g++ to use a custom c++ runtime? And I assume you can tell it to statically link it?
Tim

Re:c++ runtime

Post 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.
neowert

Re:c++ runtime

Post 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)
Tim

Re:c++ runtime

Post 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.
neowert

Re:c++ runtime

Post by neowert »

Ah. Thanks. I dont really use c++ (prefer C usually). I did not know you could write your own new/delete.
Tim

Re:c++ runtime

Post 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);
}
Post Reply