C++调用C接口
假设int test_c_fun();是C文件函数。
//test_cpp.cpp
// extern int test_c_fun(); //会链接C++函数、编译不过找不到
//写法1
#ifdef __cplusplus
extern "C"{
#endif
int test_c_fun();
int test_c_fun2();
#ifdef __cplusplus
}
#endif
//写法二
//extern "C" int test_c_fun();
C调用C++接口
C不能直接调用C++接口,需要将C++接口封装成C格式再调用,所以对外SDK啥的都是封装成C接口。
强制C++编译器使用C语言的方式来编译{}内的代码
//test_cpp.h
//test_cpp.cpp
#ifdef __cplusplus
extern "C"{
#endif
int test_cpp_fun();
#ifdef __cpluscplus
}
#endif
参考资料
https://blog.csdn.net/xuxuluo/article/details/121203445
https://blog.csdn.net/weixin_39602615/article/details/117192742