c++、oc中extern “C”的用法及代码

一、extern "C"(1)
•extern “C”的最大作用就是实现C\C++混合编程,比如C\C++的函数互调
•注意:extern后面必须是大写的C
•被extern “C”修饰的变量和函数是按照C语言方式编译和连接的
Ø也就是说extern “C” void test(int a, int b)被编译器产生的函数名就是_test,不再是类似_test_int_int之类的名称
Ø
•下面的写法是等价的:

extern "C" void test(int a, int b);

extern "C" void test2(int a, int b);

等价于

extern "C"

 

二、extern "C"(2)

•由于C语言不支持extern “C”的语法,所以严谨起见,一般C++头文件的函数声明会这么写:

#ifdef __cplusplus

extern "C"

{

#endif

   

void test(int a, int b);

void test2(int a, int b);

   

#ifdef __cplusplus

}

#endif

posted on 2013-12-21 11:46  林源  阅读(1253)  评论(0编辑  收藏  举报

导航