Page 1 of 2

Using C++ and C

Posted: Fri Aug 19, 2016 10:37 am
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.

Re: Using C++ and C

Posted: Fri Aug 19, 2016 1:20 pm
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

Re: Using C++ and C

Posted: Fri Aug 19, 2016 4:28 pm
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 "

Re: Using C++ and C

Posted: Sat Aug 20, 2016 12:34 am
by pcdever
Any idea how can I use the classes defined in c++ files .

Re: Using C++ and C

Posted: Sat Aug 20, 2016 1:17 am
by Roman
Either by compiling your C code as C++ or by creating bindings (via C global functions and opaque reference types).

Re: Using C++ and C

Posted: Sat Aug 20, 2016 1:45 am
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.

Re: Using C++ and C

Posted: Sat Aug 20, 2016 5:51 am
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..

Re: Using C++ and C

Posted: Sat Aug 20, 2016 6:57 am
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.

Re: Using C++ and C

Posted: Sat Aug 20, 2016 8:01 am
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.

Re: Using C++ and C

Posted: Sat Aug 20, 2016 8:29 am
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.

Re: Using C++ and C

Posted: Sun Aug 21, 2016 6:39 am
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 ?

Re: Using C++ and C

Posted: Sun Aug 21, 2016 7:52 am
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.

Re: Using C++ and C

Posted: Sat Aug 27, 2016 10:49 am
by pcdever
Cool , Thanks Everyone for the solution .

Re: Using C++ and C

Posted: Tue Sep 27, 2016 4:06 am
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.

Re: Using C++ and C

Posted: Tue Sep 27, 2016 9:09 am
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