cpp extern "c" keyword

 When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.

http://www.math-linux.com/spip.php?article112


extern "C" is meant to be recognized by a C++ compiler and to notify the compiler that the noted function is (or to be) compiled in C style.


Take an example, if you are working on a C++ project but it also deals with some existing C functions/libraries. 
You want to wrap them in a C++ module or compile them with other C++ objects without any C++ compiler errors, then you would declare the C function prototypes in an extern "C" block to notify the compiler that they would be compiled along with other C++ functions into one module.

For example:

my_C_CPP_Header.h:

#ifndef MY_C_CPP_HEADER
#define MY_C_CPP_HEADER


/*check if the compiler is of C++*/
#ifdef __cplusplus 
extern "C" {
int myOtherCfunc(int arg1, int arg2); /* a C function */
}
#endif

void myCppFunction1(); /* C++ function */
void myCppFunction2(); /* C++ function */

/*check if the compiler is of C++ */
#ifdef __cplusplus 
}
#endif

#endif


Now all three functions are comiled into one module by C++ compiler.

Hope you got it. Good luck :)

Related: function overload, in cpp different functions can have the same name, yet at least one of the argument should be different.

inline code of the function code generated by the function body should be inserted each time instead of inserting only once and call, which sometimes involve additional overhead.

http://www.cplusplus.com/forum/general/1143/

转载于:https://my.oschina.net/simon203/blog/106563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值