因爲C++是兼容C的,而C卻不能兼容C++,比如多態、類等等,C都是不能識別的, 所以c/c++混合編程實際上需要做的只是讓C可以調用到C++的代碼,方法就是讓C++代碼提供符合C語言規範的接口。
1 在C++頭文件中加入 extern “C"聲明:
#ifdef _cplusplus
#if _cplusplus
extern “C"
{
#endif
#endif
extern foo();
#ifdef _cplusplus
#if _cplusplus
extern “C"
}
#endif
#endif
2 在Cpp文件中接口函數前面加上 extern “C"extern “C" foo()
{
.....
}