C++调用C函数,为什么要加extern "C"?

1400阅读 0评论2016-07-25 shaohui973
分类:C/C++

   在C++程序中调用被C编译器编译后的函数,为什么要加extern "C"?

    C++语言支持函数重载,C语言不支持函数重载,函数被C++编译器编译后在
库中的名字与C语言的不同,假设某个函数原型为:
  1. void foo(int x, inty);
该函数被C编译器编译后在库中的名字为:
  1. _foo
而C++编译器则会产生像:
  1. _foo_int_int
之类的名字。为了解决此类名字匹配的问题,C++提供了C链接交换指定符号 extern "C"。


eg:


  1. #ifdef __cplusplus
  2. #if __cplusplus
  3. extern "C" {
  4. #endif
  5. #endif
  6. ...
  7. ...
  8. ...
  9. #ifdef __cplusplus
  10. #if __cplusplus
  11. }
  12. #endif
  13. #endif
上一篇:DDR原理
下一篇:dlopen与dlsym用法