Help! I've been building by OS in C and ASM for some time now, and everything is going fine. Now I find that certain crucial parts of my OS are better implemented in C++, so I want to start using it (not converting everything, just yet).
Here's the backstory:
I recently installed OSX Lion fresh, meaning I was stuck for a while without any workable compiler (for some reason I typed container 4 times instead of compiler), but since I was lazy I didn't bother.
So the problem comes when I finish my term exams and wanted to start working on my OS again. So I re-read and rebuilt the cross compiler from the wiki page, adding the c++ to enabled languages this time (didn't do it on my snowleopard install). After all those built, I got a couple of files, including i586-elf-cpp. However, I did not get g++. at all. I notice OSX has a g++, but that can't be used. I did follow everything on the wiki page, but did not end up with g++.
As a sidenote, is there a way to access C++ objects from C?
Cross compiler issue (Don't have g++)
Re: Cross compiler issue (Don't have g++)
None that doesn't involve heavy hacking, or a deliberate C layer provided by the C++ interface. I wouldn't walk that road, if I were in your shoes.requimrar wrote:As a sidenote, is there a way to access C++ objects from C?
Every good solution is obvious once you've found it.
Re: Cross compiler issue (Don't have g++)
Damn. But what about the compiler? Also, providing a thin layer of encapsulation and interfacing shouldn't be too difficult, right?Solar wrote: None that doesn't involve heavy hacking, or a deliberate C layer provided by the C++ interface. I wouldn't walk that road, if I were in your shoes.
[nx] kernel: http://github.com/zhiayang/nx
Re: Cross compiler issue (Don't have g++)
Yes, you may do something like:
However it is very ugly.
You may also hack around and call the object's member directly, with objdump you can get the function name (eg. _ZN7MyClass6DoThisEv)
You just need to make sure you work the same way with your C++ code's ABI, and don't play with virtual functions.
Code: Select all
extern "C" int MyClass_DoThis(void* the_class) {
return ((MyClass*) the_class)->DoThis();
}
You may also hack around and call the object's member directly, with objdump you can get the function name (eg. _ZN7MyClass6DoThisEv)
You just need to make sure you work the same way with your C++ code's ABI, and don't play with virtual functions.
Re: Cross compiler issue (Don't have g++)
Yes, but that's only because you went out of your way to make it ugly, instead of just doing this:extern "C" int MyClass_DoThis(void* the_class) {
return ((MyClass*) the_class)->DoThis();
}
However it is very ugly.
Code: Select all
extern "C" int MyClass_DoThis(MyClass* the_class) {
return the_class->DoThis();
}
In the C++ program:
Code: Select all
class MyClass {
virtual void __cdecl Function1();
virtual void __cdecl Function2(int param1);
int field1;
}
Code: Select all
struct MyClassVtbl {
void(__cdecl*Function1)(struct MyClass*);
void(__cdecl*Function2)(struct MyClass*,int param1);
};
struct MyClass {
MyClassVtbl*vtbl;
int field1;
};
...
MyClass*obj=GetMyClassInstanceFromSomewhere();
obj->vtbl->Function1(obj);
Re: Cross compiler issue (Don't have g++)
However, when you write the header file, since C won't recognize class MyClass, you would need something more ugly as:Gigasoft wrote:Yes, but that's only because you went out of your way to make it ugly, instead of just doing this:Code: Select all
extern "C" int MyClass_DoThis(MyClass* the_class) { return the_class->DoThis(); }
Code: Select all
#ifdef __cplusplus
class MyClass {
...
};
#else // C
struct MyClass; // forward declaration to make C happy
#endif
#ifdef __cplusplus
extern "C" {
#endif
int MyClass_DoThis(MyClass* the_class);
int MyClass_DoThat(MyClass* the_class);
...
#ifdef __cplusplus
};
#endif
IMO having multiple definition of MyClass is an bad idea.
Re: Cross compiler issue (Don't have g++)
Ah. Okay, I did download the g++ package and put it in the gcc folder, properly. I rebuilt the cross compiler, and for some reason, just sitting in the bin folder, was a i586-elf-g++... I guess something happened to it before. Thanks!
[nx] kernel: http://github.com/zhiayang/nx