Why do we need extern “C”{ #include <foo.h> } in C++?

问:

Why do we need extern “C”{ #include <foo.h> } in C++?

Specifically:

  • When should we use it?

  • What is happening at the compiler/linker level that requires us to use it? 

  • How in terms of compilation/linking does this solve the problems which require us to use it?

 

回答1:

extern "C" determines how symbols in the generated object file should be named.  If a function is declared without extern "C", the symbol name in the object file will use C++ name mangling.  Here's an example.

Given test.C like so:

void foo() { } 

Compiling and listing symbols in the object file gives:

$ g++ -c test.C 
$ nm test.o 
0000000000000000 T _Z3foov 
                 U __gxx_personality_v0 

The foo function is actually called "_Z3foov".  This string contains type information for the return type and parameters, among other things.  If you instead write test.C like this:

extern "C" { 
    void foo() { } 
} 

Then compile and look at symbols:

$ g++ -c test.C 
$ nm test.o 
                 U __gxx_personality_v0 
0000000000000000 T foo 

You get C linkage.  The name of the "foo" function in the object file is just "foo", and it doesn't have all the fancy type info that comes from name mangling.

You generally include a header within extern "C" {} if the code that goes with it was compiled with a C compiler but you're trying to call it from C++.  When you do this, you're telling the compiler that all the declarations in the header will use C linkage.  When you link your code, your .o files will contain references to "foo", not "_Z3fooblah", which hopefully matches whatever is in the library you're linking against.

Most modern libraries will put guards around such headers so that symbols are declared with the right linkage.  e.g. in a lot of the standard headers you'll find:

#ifdef __cplusplus 
extern "C" { 
#endif 
 
... declarations ... 
 
#ifdef __cplusplus 
} 
#endif 

This makes sure that when C++ code includes the header, the symbols in your object file match what's in the C library.  You should only have to put extern "C" {} around your C header if it's old and doesn't have these guards already.

 

回答2:

It has to do with the way the different compilers perform name-mangling. A C++ compiler will mangle the name of a symbol exported from the header file in a completely different way than a C compiler would, so when you try to link, you would get a linker error saying there were missing symbols.

To resolve this, we tell the C++ compiler to run in "C" mode, so it performs name mangling in the same way the C compiler would. Having done so, the linker errors are fixed.

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值