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

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
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

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

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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.
Every good solution is obvious once you've found it.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

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

Post 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?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

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

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

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

Post 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);
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

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

Post 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.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

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

Post 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!
Post Reply