C调用C++, C++调用C方法

1. C 调用 C++封装好后的函数:

-> 在C++中有一个函数 int main_cpp():

-> 首先构建头文件, #ifndef CPP_FILE_H #define CPP_FILE_H extern "C" int main_cpp(); 将C++函数按C规范编译; #endif

-> 然后在C++中, #include "cppf.h" 实现头文件中定义的函数, int main_cpp(){...}

-> 最后在C中, #include "cppf.h" 导入头文件, 直接使用main_cpp(); 即可

C++调C 看这篇文章: https://blog.csdn.net/ygsyyl/article/details/8153886

C调C++并重写其成员函数 看这篇文章: https://blog.csdn.net/nizqsut/article/details/52148973

2. 在编写C++调C的头文件中, extern "C" int main_z(); 中出现未输入表示符错误,

#ifdef __cplusplus

extern "C" {

int main_z();

}

endif

改为将C++重定义为C即可;

3. 如果是C++调C库:

例如 我们有了一个C库文件,它的头文件是f.h

extern "C"

{

#include "f.h"

}

extern "C"

{

extern void f1();

}