Extern "C"

Extern "C" is the concept that may be frequently checked during an interview.

Reference to this is an article in Baidu -

http://baike.baidu.com/link?url=m1JCaCULHSOg-b7ubUZoyJj29QtRFBO8HqnFvQ_SEAAXRV3qfcJAA8U0NFI2bSNgr_o7ZuoEDfJEq6T8k6na8_


It's not quite accurate, but it's good for a starter to look into.  Suggest you google this and read some explanation in stackover.com.


Basically, we can use C libs(C source code compiled by C compiler, e.g. GCC) and even some other supported programming languages in C++ programming, and vice versa. Focus on C and C++. 

C++ covers C, in terms of whatever standards or compilations. But C++ compiles the C/C++ in a different way from the way of C compiler. So, if we want to call the functions or interfaces that were compiled in C compiler in C++ programming, we have to point out that those interfaces were compiled by C, so that C++ compiler can put the according function symbols during the compilation and linkage will then be successful. The extern "C" implements this.


Many articles don't specify the C interfaces were already compiled by the C compiler. If we have got the C source code, we even don't need the extern "C" in C++ programming. We just use C++ compiler to recompile all c code.


In my mind, I can figure out two scenarios in which extern "C" will be necessary:

a: Some organization implemented the libs and they just provide us with the binary code(compiled via C) and the head files that expose what interfaces(function declarations) are implemented in their libs.

Then we can include such head files, and specify the extern "C" in C++ programming.

b: Even we have got the C source code and we have re-compile them in our C++ development environment, we need to move or integrate the C compile-environment to our C++ ongoing compile-environment. This will add to our complexity.

So, to keep it simple, we can separate the C and C++ compile-environment. Still use the original C compile-environment and build out the binary code, then just link them to the C++ program.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值