test_cpp.cpp
------------------------------------------------------
#include <stdio.h>
// C++调用C函数的方法,将用到的函数全部重新声明一遍
extern "C"
{
extern int c_funcA();
}
// C调用C++函数的方法
extern "C" int cpp_funcA()
{
printf ("cpp_funcA\n");
return 0;
}
extern "C" int cpp_funcB()
{
printf ("cpp_funcB\n");
return 0;
}
int main()
{
c_funcA();
cpp_funcB();
return 0;
}
test_c.c
------------------------------------------------------
int cpp_funcA();
int c_funcA()
{
cpp_funcA();
return 0;
------------------------------------------------------
#include <stdio.h>
// C++调用C函数的方法,将用到的函数全部重新声明一遍
extern "C"
{
extern int c_funcA();
}
// C调用C++函数的方法
extern "C" int cpp_funcA()
{
printf ("cpp_funcA\n");
return 0;
}
extern "C" int cpp_funcB()
{
printf ("cpp_funcB\n");
return 0;
}
int main()
{
c_funcA();
cpp_funcB();
return 0;
}
test_c.c
------------------------------------------------------
int cpp_funcA();
int c_funcA()
{
cpp_funcA();
return 0;
}
C++是后来者,所以特殊处理都在CPP中做
本文介绍了一种C与C++互相调用的具体实现方式。通过示例代码展示了如何从C++调用C函数,并从C代码中调用C++函数。重点在于正确使用extern C来确保符号的兼容性。

被折叠的 条评论
为什么被折叠?



