User Tools

Site Tools


codebase:add_cpp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codebase:add_cpp [2017/05/25 03:10] vodurcodebase: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/enforce bool compatibility between C and C++.
 +  * Otherwise, the general mechanics for C/C++ interop apply (need to use extern "C" on C++ side).
 +
 +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://www.geeksforgeeks.org/write-c-program-produce-different-result-c/|http://www.geeksforgeeks.org/write-c-program-produce-different-result-c/]] [[http://www.geeksforgeeks.org/write-c-program-produce-different-result-c/|http://www.geeksforgeeks.org/write-c-program-produce-different-result-c/]]
  
-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 "C" { }). To access C functions from C++, C++ code will need to include the C headers (wrapping in extern "C" { }).
  
-To access C++ functions from C, C++ needs to define the function inside extern "C" { }.+To access C++ functions from C, C++ needs to declare the function inside extern "C" { }.
  
 The extern "C" { } forces C++ to use C naming convention in object file (instead of doing C++ name mangling), which allows the C++ and C objects to link properly. The extern "C" { } forces C++ to use C naming convention in object file (instead of doing C++ name mangling), which allows the C++ and C objects to link properly.
  
-So we have to make sure that any header file used by both C and C++ is compatible with bothFor existing C files the big sticking points will be use of C++ keywordsRenaming can clear most of this up, but bool typedefs is another story.+[[https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B|https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B]]
  
-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? AnswerI think so...+[[https://stackoverflow.com/questions/8284167/nested-structures-in-c-and-c|https://stackoverflow.com/questions/8284167/nested-structures-in-c-and-c]]
  
-<code> +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.
-:geshi C+++
  
 +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…
 +
 +<code C++>
 #ifndef BOOLTYPE_H #ifndef BOOLTYPE_H
 #define BOOLTYPE_H #define BOOLTYPE_H
Line 81: Line 91:
 </code> </code>
  
-[[https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B|https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B]]+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 GCCWe can even add the static asserts as above just for some peace of mind.
  
-[[https://stackoverflow.com/questions/8284167/nested-structures-in-c-and-c|https://stackoverflow.com/questions/8284167/nested-structures-in-c-and-c]]+[[https://bitbucket.org/aarchon/mud_src/commits/branch/CPP_COMPAT|https://bitbucket.org/aarchon/mud_src/commits/branch/CPP_COMPAT]]
  
  
codebase/add_cpp.1495681813.txt.gz · Last modified: 2017/05/25 03:10 by vodur