Alternate linkage specifications

from <thinking in C++>

Alternate linkage specifications

What happens if you’re writing a program in C++ and you want to 
use a C library? If you make the C function declaration, 
float f(int a, char b); 
the C++ compiler will decorate this name to something like 
_f_int_charto support function overloading (and type-safe 
linkage). However, the C compiler that compiled your C library has 
most definitely notdecorated the name, so its internal name will be 
_f. Thus, the linker will not be able to resolve your C++ calls to f( ). 
The escape mechanism provided in C++ is the alternate linkage 
specification, which was produced in the language by overloading 
the externkeyword. The externis followed by a string that 
466 Thinking in C++  www.BruceEckel.com 
specifies the linkage you want for the declaration, followed by the 
declaration: 
extern "C" float f(int a, char b); 
This tells the compiler to give C linkage to f( )so that the compiler 
doesn’t decorate the name. The only two types of linkage 
specifications supported by the standard are “C”and “C++,”but 
compiler vendors have the option of supporting other languages in 
the same way. 
If you have a group of declarations with alternate linkage, put them 
inside braces, like this: 
extern "C" { 
float f(int a, char b); 
double d(int a, char b); 

Or, for a header file, 
extern "C" { 
#include "Myheader.h" 

Most C++ compiler vendors handle the alternate linkage 
specifications inside their header files that work with both C and 
C++, so you don’t have to worry about it
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值