c与cpp的相互混合应用(c嵌入到cpp里或者cpp嵌入到c里) extern c和_cplusplus(一般在c库里合用)的来历
2013-11-29 10:11
375 查看
Linkage Directives: extern "C"
C++ programs sometimes need to call functions written in another programming language. Most often, that other language is C. Like any name, the name of a function written in another language must be declared. That declaration must specify
the return type and parameter list. The compiler checks calls to external-language functions in the same way that it handles ordinary C++ functions. However, the compiler typically must generate different code to call functions written in other languages.
C++ uses linkage directives to indicate the language used for any non-C++ function.
Declaring a Non-C++ Function
A linkage directive can have one of two forms: single or compound. Linkage directives may not appear inside a class or function definition. The linkage directive must appear on the first declaration of a function.
As an example, let's look at some of the C functions declared in the
cstdlib header. Declarations in that header might look something like
// illustrative linkage directives that might appear in the C++ header <cstring>
// single statement linkage directive
extern "C" size_t strlen(const char *);
// compound statement linkage directive
extern "C" {
int strcmp(const char*, const char*);
char *strcat(char*, const char*);
}
The first form consists of the extern keyword followed by a string literal, followed by an "ordinary" function declaration. The string literal indicates the language in which the function is written.
We can give the same linkage
to several functions at once by enclosing their declarations inside curly braces following the linkage directive. These braces serve to group the declarations to which the linkage directive applies. The braces are otherwise ignored, and the names of functions
declared within the braces are visible as if the functions were declared outside the braces.
Preprocessor Support for Linking to C——_cplusplus的来历
Itcan be useful sometimes to compile the same source file in both C or C++. The preprocessor name
__cplusplus (two underscores) is automatically defined when compiling C++, so we can conditionally include code based on whether we are compiling C++.
#ifdef __cplusplus // ok: we're compiling C++ extern "C" #endif int strcmp(const char*, const char*);
相关文章推荐
- 移动应用或者手机游戏现在服务端的架构一般都是怎么样的
- xcode5编写ios7应用中UITableView、UIWebView嵌入UIScrollView混合编程心得
- 在Grails1.2.2应用中嵌入JBPM4.3(2)
- 在C应用中嵌入http服务器libmicrohttp
- Appium测试混合应用失败后截图注意事项
- web应用中嵌入百度地图
- 一般数据语句或者建表问题:yiorg.springframework.dao.InvalidDataAccessResourceUsageException:
- Android应用的回收机制或者怎样让应用尽量不被kill
- cocos2d在IOS嵌入UM应用推荐
- 【Android】打开后台运行或者未开启的应用
- php文件之间相互引用路径问题的一般处理方法
- C/C++混合编译和相互调用
- 可以从应用服务器上传或者下载文件的T-code
- NanoHttpd 专门为嵌入java应用设计的轻型HTTP服务器
- 在一个tomcat上配置多个相同的应用,或者是用同一个端口上放置多个应用
- 避免网络应用死机,崩溃,保证服务质量的一般方法
- Oracle “CONNECT BY” 使用Oracle “CONNECT BY”是层次查询子句,一般用于树状或者层次结果集的查询。其语法是:
- android 原生应用、Web应用、混合应用优缺点分析
- 交叉编译多个cpp或者c文件自动构建Makefile
- android/iPhone:如何从browser直接打开应用程序或者没有应用程序打开应用商店