Using C++ and C

Programming, for all ages and all languages.
pcdever
Posts: 17
Joined: Thu Jul 28, 2016 1:23 am
Libera.chat IRC: pcdever

Using C++ and C

Post by pcdever »

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.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Using C++ and C

Post by max »

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: Select all

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
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: Using C++ and C

Post by Boris »

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 "
pcdever
Posts: 17
Joined: Thu Jul 28, 2016 1:23 am
Libera.chat IRC: pcdever

Re: Using C++ and C

Post by pcdever »

Any idea how can I use the classes defined in c++ files .
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Using C++ and C

Post by Roman »

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
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Using C++ and C

Post by simeonz »

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.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Using C++ and C

Post by max »

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..
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Using C++ and C

Post by simeonz »

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.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Using C++ and C

Post by max »

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.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Using C++ and C

Post by Roman »

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
pcdever
Posts: 17
Joined: Thu Jul 28, 2016 1:23 am
Libera.chat IRC: pcdever

Re: Using C++ and C

Post by pcdever »

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 ?
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Using C++ and C

Post by max »

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.
pcdever
Posts: 17
Joined: Thu Jul 28, 2016 1:23 am
Libera.chat IRC: pcdever

Re: Using C++ and C

Post by pcdever »

Cool , Thanks Everyone for the solution .
whamscalene424
Posts: 1
Joined: Sat Sep 24, 2016 9:01 am

Re: Using C++ and C

Post by whamscalene424 »

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.
Lowl3v3l
Posts: 8
Joined: Mon Aug 29, 2016 12:33 pm
Libera.chat IRC: Lowl3v3l

Re: Using C++ and C

Post by Lowl3v3l »

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