Let C plus plus compatible with C
linD026 opened this issue · comments
If we want to let C++ compatible with C, we need to do the following things:
/* header */
#ifdef __cplusplus
extern "C" {
#endif
void C_function();
#ifdef __cplusplus
}
#endif
The more information from Stack Overflow:
- extern "C" is a linkage-specification
- Every compiler is required to provide "C" linkage
- A linkage specification shall occur only in namespace scope
- See Richard's Comment: Only function names and variable names with external linkage have a language linkage
- Two function types with distinct language linkages are distinct types even if otherwise identical
- Linkage specs nest, inner one determines the final linkage
- extern "C" is ignored for class members
- At most one function with a particular name can have "C" linkage (regardless of namespace)
- See Richard's comment: static inside extern "C" is valid; an entity so declared has internal linkage, and so does not have a language linkage
- Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved
Here is the cppreference: Language linkage
We need to change all the current headers to the prior mentioned.
I will take the job.