《系统程序员成长计划》学习交流(5)-extend "C"

【问题描述】

常见如下代码:

/*
 * File:    dlist.h
 * Author:  Li XianJing <xianjimli@hotmail.com>
 * Brief:   double list header file.
 *
 * Copyright (c) Li XianJing
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2008-11-09 Li XianJing <xianjimli@hotmail.com> created
 *
 */

#ifndef DLIST_H
#define DLIST_H

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus*/

typedef enum _DListRet
{
	DLIST_RET_OK,
	DLIST_RET_OOM,
	DLIST_RET_STOP,
	DLIST_RET_PARAMS,
	DLIST_RET_FAIL
}DListRet;

struct _DList;
typedef struct _DList DList;

typedef DListRet (*DListDataPrintFunc)(void* data);

DList* dlist_create(void);

DListRet dlist_insert(DList* thiz, size_t index, void* data);
DListRet dlist_prepend(DList* thiz, void* data);
DListRet dlist_append(DList* thiz, void* data);
DListRet dlist_delete(DList* thiz, size_t index);
DListRet dlist_get_by_index(DList* thiz, size_t index, void** data);
DListRet dlist_set_by_index(DList* thiz, size_t index, void* data);
size_t   dlist_length(DList* thiz);
DListRet dlist_print(DList* thiz, DListDataPrintFunc print);

void dlist_destroy(DList* thiz);

#ifdef __cplusplus
}
#endif/*__cplusplus*/

#endif/*DLIST*/


下面这一段中,extend "C"该如何理解呢?

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus*/

...

#ifdef __cplusplus
}
#endif/*__cplusplus*/


【分析】

1 如果没有定义__cplusplus宏,那么源文件相当于(代码1):

#ifndef DLIST_H
#define DLIST_H

typedef enum _DListRet
{
	DLIST_RET_OK,
	DLIST_RET_OOM,
	DLIST_RET_STOP,
	DLIST_RET_PARAMS,
	DLIST_RET_FAIL
}DListRet;

struct _DList;
typedef struct _DList DList;

typedef DListRet (*DListDataPrintFunc)(void* data);

DList* dlist_create(void);

DListRet dlist_insert(DList* thiz, size_t index, void* data);
DListRet dlist_prepend(DList* thiz, void* data);
DListRet dlist_append(DList* thiz, void* data);
DListRet dlist_delete(DList* thiz, size_t index);
DListRet dlist_get_by_index(DList* thiz, size_t index, void** data);
DListRet dlist_set_by_index(DList* thiz, size_t index, void* data);
size_t   dlist_length(DList* thiz);
DListRet dlist_print(DList* thiz, DListDataPrintFunc print);

void dlist_destroy(DList* thiz);

#endif/*DLIST*/


如果定义了__cplusplus宏,那么源文件相当于(代码2):

#ifndef DLIST_H
#define DLIST_H

extern "C" {

typedef enum _DListRet
{
	DLIST_RET_OK,
	DLIST_RET_OOM,
	DLIST_RET_STOP,
	DLIST_RET_PARAMS,
	DLIST_RET_FAIL
}DListRet;

struct _DList;
typedef struct _DList DList;

typedef DListRet (*DListDataPrintFunc)(void* data);

DList* dlist_create(void);

DListRet dlist_insert(DList* thiz, size_t index, void* data);
DListRet dlist_prepend(DList* thiz, void* data);
DListRet dlist_append(DList* thiz, void* data);
DListRet dlist_delete(DList* thiz, size_t index);
DListRet dlist_get_by_index(DList* thiz, size_t index, void** data);
DListRet dlist_set_by_index(DList* thiz, size_t index, void* data);
size_t   dlist_length(DList* thiz);
DListRet dlist_print(DList* thiz, DListDataPrintFunc print);

void dlist_destroy(DList* thiz);

}

#endif/*DLIST*/


__cplusplus宏是C++编译器定义的宏,也就是说C++程序编译时,该宏就存在了。即如果dlist.h被C程序包含,预处理为代码1,被C++程序包含,预处理为代码2。

 

2 C语言中不能识别

extern "C"
{

}

结构。C++程序可以识别该结构。所以如果在C中采用该结构是错误的。这就是为什么要采用宏#ifdef __cplusplus判断的原因。

 

3 关于extern "C"

为什么要使用extern "C"呢?看一个例子:

void foo(int x, int y);

C++支持重载,也就是说可以在同样的命名空间内,出现同函数名,不同参数的情况,如:

void foo(int x, int y);
void foo(int x, float y);

可以同时出现在同一类中。重载的特性给C++程序带来了便利。遗憾的是,C并不支持该特性。

(1) 重载的实现

重载是如何实现的呢?其实C++编译器在编译阶段做了“手脚”,它将

void foo(int x, int y);

解释为类似_foo_int_int的汇编代码

void foo(int x,float y);

解释为_foo_int_float。这样就可以识别重载的情况了。但C并不这么认为,在编译时,它将

void foo(int x, int y);

void foo(int x,float y);

都解释为类似_foo汇编代码。因此,如果在C中直接使用C++重载特性,编译就会报错。因为重载的函数,都被解释为同一个汇编代码,重载无法识别。

(2) 链接库的思考

现在,假设已经编译了一个C的动态链接库。并且提供了函数接口。用C调用它,当然不会出现问题。当如果想用C++调用该库文件呢?void foo(int x, int y);被解释为_foo_int_int。而链接库中,void foo(int x, int y);被编译为_foo。这时编译器,会提示找不到foo函数,实质上,我们确实已经用C实现了foo函数。

(3) 重写代码 or ?

要不要重写库代码呢?答案是不需要。C++有个聪明的办法,那就是将C的代码放在

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus*/

...

#ifdef __cplusplus
}
#endif/*__cplusplus*/

结构中。它提醒如果C++调用该段代码,编译器不能对函数名进行重新编码,即采用C的方式进行处理。这样在C++中,void foo(int x, int y)被编译为_foo,这样提供了“正确的接口”,链接库就可以被C++程序识别了。

(4) 在C中调用C++

如果想在C中调用C++该怎么办呢?方法是在C++程序中(cpp文件中),要调用的函数外部,加入如下例所示结构:

extern "C"
{
	void foo(int x, int y);
}


这样C++按照C的方法进行处理,就不会出现函数名不一致的问题了。

 

 

【学习参考】

C++项目中的extern "C" {}

 

 

 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

 

 

 

 

 

 

 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值