extern "C"

来源:互联网 发布:郑州淘宝抓了人员名单 编辑:程序博客网 时间:2024/05/09 06:46
//C++ Primer 第四版

1,C++使用链接指示(linkage directive)指出任意非C++函数所用的语言。
2,链接指示有两种形式:单个的或复合的。链接指示不能出现在类定义或函数定义的内部,它必须出现在函数的第一次声明上。
3,声明非C++函数:
extern "C" size_t strlen(const char *);
extern "C"{
    int strcmp(const char*,const char*);
    char* strcat(char*,const char*);
}
4,可以将多重声明形式应用与整个头文件。例如,C++的cstring头文件可以像这样:
extern "C" {
#include<string.h>
}
当将#include指示放在复合链接指示的花括号的时候,假定头文件中的所有普通函数声明都是用链接指示的语言编写的函数。链接指示可以嵌套,所以,如果头文件包含了带链接指示的函数,该函数的链接不受影响。
5,Linkage Direcives Apply to the Entire Declaration:
When we use a linkage directive,it applies to the function and any function pointers used as the return type or as a parameter teype:
//f1 is a function ;its parameter is a pointer to a C function.
extern "C" void f1(void (*)(int) );
This declaration says that f1 is a C function that doesn't return a value. It has one parameter,which is a pointer to a function that return s nothing and takes a single int parameter. The linkage directive applies to the function pointer as well as to f1.When we call f1,we must pass it the name of a C function or a pointer to a C function.
Because a linkage directive applies to all the functions in a declaration ,we must use a typedef to pass a pointer to a C function to a C++ function:
//FC is a pointer to C function
extern "C" typedef void FC(void);
//f2 is a C++function with a parameter that is a pointer to a C function
void f2(FC *);

0 0
原创粉丝点击