C++ 为什么要使用#ifdef __cplusplus extern "C" { #endif
转载:http://www.cnblogs.com/ayanmw/archive/2012/03/15/2398593.html
转载:http://blog.csdn.net/zkl99999/article/details/48134621
转载: http://www.jianshu.com/p/5d2eeeb93590
经常看到别人的头文件 有这样的代码
#ifdef __cplusplus
extern "C" {
#endif // C 样式 的函数 #ifdef __cplusplus
}
#endif
为什么要这样呢?
因为C语言不支持重载函数,也就是同名函数,参数却不一样,C++支持,其编译器对函数名的处理方法不一样,导致 虽然都是C 样式的函数,不同编译器编译出来的不一样。
如果是C语言编译的中间文件,要C++ 来调用,那么就需要这个了,C++ 有了 extern "C" 就会按照 C 语言的方法进行函数命名。这样编译出来的中间文件就是C样式的函数名,C/C++ 都可以调用。
如果C++ 编译的中间文件,要C语言来调用,是不行的。这也就为什么DLL中常看见extern "C" {},windows是采用C语言编制他首先要考虑到C可以正确调用这些DLL,而用户可能会使用C++而extern "C" {}就会发生作用。
总结:
1.extern "C"表示编译生成的内部符号名使用C约定。
2.C++支持函数重载,而C不支持,两者的编译规则也不一样,函数被C++编译后在符号库中的名字与C语言的不同。
例如,假设某个函数的原型为: void foo( int x, int y ); 该函数被C编译器编译后在符号库中的名字可能为_foo,而C++编译器则会产生像_foo_int_int之类的名字(不同的编译器可能生成的名字不 同,但是都采用了相同的机制,生成的新名字称为“mangled name”)。_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息,C++就是靠这种机制来实现函数重载的。
例子:
test.h
#ifndef _TEST_H_
#define _TEST_H_
#ifdef _cplusplus
extern "c"
{
#endif
extern int Add(int a, int b);
extern int Sub(int a, int b); #ifdef _cplusplus
}
#endif #endif
test.c
#include "test.h" int Add(int a, int b)
{
return a + b;
} int Sub(int a, int b)
{
return a - b;
}
main.cpp
#include <stdio.h> extern "C"
{
#include "test.h"
} int main()
{
printf("Add=%d\n",Add(,));
printf("Sub=%d\n",Sub(,));
return ;
}
C++ 为什么要使用#ifdef __cplusplus extern "C" { #endif的更多相关文章
- #ifdef __cplusplus extern "C" { #endif
1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus extern "C" { #endif //c语法代码段 #ifdef __ ...
- #ifdef __cplusplus extern c #endif 的作用
#ifdef __cplusplus // C++编译环境中才会定义__cplusplus (plus就是"+"的意思) extern "C" { // 告诉编 ...
- “#ifdef __cplusplus extern "C" { #endif”的定义
平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #end ...
- “#ifdef __cplusplus extern "C" { #endif”的定义-----C和C++的互相调用
"#ifdef __cplusplus extern "C" { #endif"的定义 看一些程序的时候老是有 "#ifdef __cplusplus ...
- #ifdef __cplusplus extern "C" { #endif”的定义
平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #e ...
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...
- #ifdef __cplusplus extern "C" { #endif 的解释
好多程序中都会遇到下列代码段: #ifdef __cplusplus extern "C" { #endif /****************** C语法代码段 ******** ...
- 备忘录:“#ifdef __cplusplus extern "C" { #endif”的定义
看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...
- undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif
最近在弄一个进程间通信,原始测试demon用c语言写的,经过测试ok,然后把接口封装起来了一个send,一个recv. 使用的时候send端是在一个c语言写的http服务端使用,编译ok没有报错,但是 ...
随机推荐
- Android UsageStats:应用根据启动次数、启动时间、应用名称排序
Android 7.1.1 developers/samples/android/system/AppUsageStatistics/Application/src/main/java/com/exa ...
- 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树
[BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...
- LCA(离线算法)
hdu4547 CD操作 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- 树链剖分-点的分治(链的点的个数为k的点对数)
hdu4760 Cube number on a tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 ...
- log4j.properties配置详解与实例(转载)
转自:http://blog.sina.com.cn/s/blog_5ed94d710101go3u.html 最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛 ...
- PHP新版本变化
世界变化真快,突然听闻 PHP 都到 7.3 版本了,7.2 还没仔细了解过呢.看到我司面试时会问到php新版本有什么特性,美名其曰考察其学习新技术的能力,我有点汗颜,自己都没有主动去了解过,实在不应 ...
- PHP 的“魔术常量”
名称 说明 __LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路 ...
- 组织机构代码校验码生成算法(C#版)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 快递API接口
快递100
- 解决Android中ListView列表只显示一项数据的问题
思路:获取每项item的高度,并相加,再加上分割线的高度,作为整个ListView的高度,方法如下: public static void setListViewHeightBasedOnChildr ...