Page 1 of 1

Cross compiler issue (Don't have g++)

Posted: Sat Mar 17, 2012 7:53 am
by zhiayang
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?

Re: Cross compiler issue (Don't have g++)

Posted: Sat Mar 17, 2012 8:23 am
by Solar
requimrar wrote:As a sidenote, is there a way to access C++ objects from C?
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.

Re: Cross compiler issue (Don't have g++)

Posted: Sat Mar 17, 2012 8:26 am
by zhiayang
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.
Damn. But what about the compiler? Also, providing a thin layer of encapsulation and interfacing shouldn't be too difficult, right?

Re: Cross compiler issue (Don't have g++)

Posted: Sat Mar 17, 2012 12:40 pm
by bluemoon
Yes, you may do something like:

Code: Select all

extern "C" int MyClass_DoThis(void* the_class) {
  return ((MyClass*) the_class)->DoThis();
}
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.

Re: Cross compiler issue (Don't have g++)

Posted: Sat Mar 17, 2012 2:55 pm
by Gigasoft
extern "C" int MyClass_DoThis(void* the_class) {
return ((MyClass*) the_class)->DoThis();
}
However it is very ugly.
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();
}
Here is an example of accessing virtual functions from C (assuming a standard x86 compiler run with default options, no RTTI, no multiple inheritance etc):

In the C++ program:

Code: Select all

class MyClass {
    virtual void __cdecl Function1();
    virtual void __cdecl Function2(int param1);
    int field1;
}
In the C program:

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++)

Posted: Sat Mar 17, 2012 3:25 pm
by bluemoon
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();
}
However, when you write the header file, since C won't recognize class MyClass, you would need something more ugly as:

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++)

Posted: Mon Mar 19, 2012 3:29 am
by zhiayang
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++... :oops: I guess something happened to it before. Thanks!