c/c++混合编程简明总结

来源:互联网 发布:毕业设计查重软件 编辑:程序博客网 时间:2024/05/16 03:13

因为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()

{

.....

}