Using C++ and C
Using C++ and C
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.
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.
- max
- Member
- Posts: 616
- Joined: Mon Mar 05, 2012 11:23 am
- Libera.chat IRC: maxdev
- Location: Germany
- Contact:
Re: Using C++ and C
Hello,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.
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() {}
Greets
Re: Using C++ and C
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 "
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
Any idea how can I use the classes defined in c++ files .
Re: Using C++ and C
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
- Alan Kay
Re: Using C++ and C
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.
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.
- max
- Member
- Posts: 616
- Joined: Mon Mar 05, 2012 11:23 am
- Libera.chat IRC: maxdev
- Location: Germany
- Contact:
Re: Using C++ and C
By compiling all your code with C++ compiler and then simply including your headers that define your classes wherever you want to use them..pcdever wrote:Any idea how can I use the classes defined in c++ files .
Re: Using C++ and C
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.
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.
- max
- Member
- Posts: 616
- Joined: Mon Mar 05, 2012 11:23 am
- Libera.chat IRC: maxdev
- Location: Germany
- Contact:
Re: Using C++ and C
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.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.
Re: Using C++ and C
Not just C users, C is lingua franca of programming, i.e. de facto FFI standard.Max wrote:want C users to be able to use it
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
- Alan Kay
Re: Using C++ and C
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 ?
- max
- Member
- Posts: 616
- Joined: Mon Mar 05, 2012 11:23 am
- Libera.chat IRC: maxdev
- Location: Germany
- Contact:
Re: Using C++ and C
Yes.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 ?
Re: Using C++ and C
Cool , Thanks Everyone for the solution .
-
- Posts: 1
- Joined: Sat Sep 24, 2016 9:01 am
Re: Using C++ and C
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.
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
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
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