#ifndef __APT_PTHREAD_H__
#define __APT_PTHREAD_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <stdio.h>
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __APT_PTHREAD_H__ */
1.防止头文件重复包含
#ifndef _FILE_H_
#define _FILE_H_...
#endif
2.c 与c++ 互调时的c函数声明
当我们想从C++中调用C的库时,不能仅仅声明一个外部函数,因为调用C函数的编译代码和调用C++函数的编译代码是不同的。
如果你仅说明一个外部函数, C++编译器假定它是C++的函数编译成功了,但当你连接时会发现错误。这是因为编译方法的不同造成的。
C++支持函数重载,而C不支持,两者的编译规则也不一样。
函数被C++编译后在符号库中的名字与C语言的不同。
例如,假设某个函数的原型为: void foo( int x, int y );
该函数被C编译器编译后在符号库中的名字可能为_foo,而C++编译器则会产生像_foo_int_int之类的名字
(不同的编译器可能生成的名字不同,但是都采用了相同的机制,生成的新名字称为“mangled name”)。
_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息,C++就是靠这种机制来实现函数重载的。
解决这种问题的方法就是指定它为C函数:
extern "C" 函数描述
extern "C"表示编译生成的内部符号名使用C约定
指定一群函数的话:
extern "C"{
n个函数描述
}
如果想C和C++混用的话:
#ifdef _cplusplus
extern "C"{
#endif
n个函数描述
#ifdef _cplusplus
}
#endif
下面以例子说明,如何在C++中使用C的函数,
//C++引用C函数的例子
//test.c
#include <stdio.h>
void mytest()
{
printf("mytest in .c file ok\n");
}
//main.cpp
extern "C"
{
void mytest();
}
int main()
{
mytest();
return 0;
}
参考文章
http://hi.baidu.com/mayday428/blog/item/5d9f5ccf5839600100e9285c.html