OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 6:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Using C++ and C
PostPosted: Fri Aug 19, 2016 10:37 am 
Offline

Joined: Thu Jul 28, 2016 1:23 am
Posts: 17
Hello everyone ,
I am presently coding my os in C and very comfortable with it .But I was thinking of using C++ too in my os .
My question is how could I manage to link a C++ program into my present C kernel and use the classes , functions and everything defined in my C++ files from my C files.

Any other guidance on this topic would me appreciated.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Fri Aug 19, 2016 1:20 pm 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 616
Location: Germany
pcdever wrote:
Hello everyone ,
I am presently coding my os in C and very comfortable with it .But I was thinking of using C++ too in my os .
My question is how could I manage to link a C++ program into my present C kernel and use the classes , functions and everything defined in my C++ files from my C files.

Any other guidance on this topic would me appreciated.

Thanks in advance.


Hello,

just compile all your code with a C++ compiler and you're good.

If you need C linkage for one of your functions then, you can tell the compiler so:
Code:
extern "C" void myFunction() {}


Note that you will not be able to use C++ features like exceptions or anything that requires RTTI, because you need a runtime for this. All basic features work fine though.

Greets

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Fri Aug 19, 2016 4:28 pm 
Offline
Member
Member

Joined: Sat Nov 07, 2015 3:12 pm
Posts: 145
Hi,
As stated above , you can't directly call c++ functions from C code . You have to create in a cpp file, a c function that calls your c++ code. Such functions are usually called " C binding " or " c wrapper "


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 12:34 am 
Offline

Joined: Thu Jul 28, 2016 1:23 am
Posts: 17
Any idea how can I use the classes defined in c++ files .


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 1:17 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
Either by compiling your C code as C++ or by creating bindings (via C global functions and opaque reference types).

_________________
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 1:45 am 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
The pedantic way to bridge C and C++ code is to create wrapper functions declared with the extern "C" keyword within the C++ files. Those have to take pointer arguments for any structure whose declaration the C code will not be able to see. This means for all non-POD structures. For all non-POD structures you will have to use dynamic memory allocation through the C++ code, because you will only have forward declarations or void for those. That is, the compiler will not be aware of the type size, and it will not be able to allocate stack or static storage while code generating from the C sources.

Technically, depending on your compiler, you could engineer something highly non-standard. You could generate opaque storage structures that have the necessary alignment and size (extracted automatically using some pre-build step) or bind weak symbols to the decorated method names (again preferably using Python as code generator for example.) It is doable, but is some additional non-portable hassle.


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 5:51 am 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 616
Location: Germany
pcdever wrote:
Any idea how can I use the classes defined in c++ files .
By compiling all your code with C++ compiler and then simply including your headers that define your classes wherever you want to use them..

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 6:57 am 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
I should say, that max's suggestion is probably best.

Wrappers (which I suggested) are for a different use case, when you need to use someone's non-C++ compliant C headers or code base. But your code will most likely compile as C++ just fine, or will require minimal adjustments. Basically, the C code you have written so far is C++ code as well, syntactically speaking.


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 8:01 am 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 616
Location: Germany
simeonz wrote:
Wrappers (which I suggested) are for a different use case, when you need to use someone's non-C++ compliant C headers or code base. But your code will most likely compile as C++ just fine, or will require minimal adjustments. Basically, the C code you have written so far is C++ code as well, syntactically speaking.
Correct, thr normal use case is when you write a C++ library and want C users to be able to use it. You then define your functions as external "C" and write C-compliant headers and then they can use them when compiling with a C compiler.

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 20, 2016 8:29 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
Max wrote:
want C users to be able to use it
Not just C users, C is lingua franca of programming, i.e. de facto FFI standard.

_________________
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sun Aug 21, 2016 6:39 am 
Offline

Joined: Thu Jul 28, 2016 1:23 am
Posts: 17
Well , then as far as i can understand , i will be writing the same C codes with a minor adjustments (if needed ) but compile them with g++ , right ?


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sun Aug 21, 2016 7:52 am 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 616
Location: Germany
pcdever wrote:
Well , then as far as i can understand , i will be writing the same C codes with a minor adjustments (if needed ) but compile them with g++ , right ?
Yes.

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Sat Aug 27, 2016 10:49 am 
Offline

Joined: Thu Jul 28, 2016 1:23 am
Posts: 17
Cool , Thanks Everyone for the solution .


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Tue Sep 27, 2016 4:06 am 
Offline

Joined: Sat Sep 24, 2016 9:01 am
Posts: 1
I have a question:

I want to know if it's feasible to write a Kernel in C++ and if not, what would be the drawbacks.


Top
 Profile  
 
 Post subject: Re: Using C++ and C
PostPosted: Tue Sep 27, 2016 9:09 am 
Offline

Joined: Mon Aug 29, 2016 12:33 pm
Posts: 8
Well its possible to write a kernel in C++, though you'd be pretty limited in which language features you can use. Exceptions, everything that requires RTTI, the standard library along with STL, global and static objects and some additional things need support code that you'd need to provide first or that are not useful in a kernel anyway. Another thing is : C++ can do lot of Magic that may or may not make debugging harder than a C kernel. But it certainly is possible, as i said.

The better question for me is : in a ( most likely rather small kernel) : is it worth it? Do you want to use specific features to C++ in such an order of magnitude that it is necessary? Some project leaders especially in Microkernels have concluded that using C++ wouldn't add anything to their kernel so they stick with C because its easier to supply all needed code for it and it does less magic. But this is a choice you'd have to do on your own, just remind that object oriented programming can have severe performance penalties, especially for something like IPC in a kernel, so look for bottlenecks if you decide to use C++.

have a nice day


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 41 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group