codebase:add_cpp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
codebase:add_cpp [2017/05/25 03:12] – vodur | codebase:add_cpp [2019/07/13 06:16] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | TL;DR: | ||
+ | |||
+ | * Need to fix C headers to not have C++ keywords. | ||
+ | * Need to add code to verify/ | ||
+ | * Otherwise, the general mechanics for C/C++ interop apply (need to use extern " | ||
+ | |||
+ | Notes and details below. | ||
+ | |||
Trying to just compile existing C code as C++ will likely cause many errors and may have potential functional differences (not likely, but possible). | Trying to just compile existing C code as C++ will likely cause many errors and may have potential functional differences (not likely, but possible). | ||
Line 9: | Line 17: | ||
[[http:// | [[http:// | ||
- | C/C++ interop is well supported. We can compile C files as C into object files and C as C++ into object files and link them without issue. | + | C/C++ interop is well supported. We can compile C files as C into object files and C++ as C++ into object files and link them without issue. |
To access C functions from C++, C++ code will need to include the C headers (wrapping in extern " | To access C functions from C++, C++ code will need to include the C headers (wrapping in extern " | ||
- | To access C++ functions from C, C++ needs to define | + | To access C++ functions from C, C++ needs to declare |
The extern " | The extern " | ||
- | |||
- | So we have to make sure that any header file used by both C and C++ is compatible with both. For existing C files the big sticking points will be use of C++ keywords. Renaming can clear most of this up, but bool typedefs is another story. | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
+ | |||
+ | So we have to make sure that any header file used by both C and C++ is compatible with both. For existing C files the big sticking points will be use of C++ keywords. Renaming can clear most of this up, but bool typedefs is another story. | ||
In C++ bool is a keyword and built in type. In C we just typedef it. Can we somehow make C bool and C++ bool 100% compatible, such that we can guarantee the size is the same and we can pass bool values between C and C++ code without worrying about strange or undefined behavior? Answer: I think so… | In C++ bool is a keyword and built in type. In C we just typedef it. Can we somehow make C bool and C++ bool 100% compatible, such that we can guarantee the size is the same and we can pass bool values between C and C++ code without worrying about strange or undefined behavior? Answer: I think so… | ||
Line 82: | Line 90: | ||
#endif | #endif | ||
</ | </ | ||
+ | |||
+ | Basically, C++ is not guaranteed to be implemented as a char but it generally is, and almost certainly is and always will be when using GCC. We can even add the static asserts as above just for some peace of mind. | ||
+ | |||
+ | [[https:// | ||
codebase/add_cpp.1495681940.txt.gz · Last modified: 2017/05/25 03:12 by vodur