转载: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的更多相关文章

  1. #ifdef __cplusplus extern "C" { #endif

    1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus        extern "C" {        #endif //c语法代码段 #ifdef __ ...

  2. #ifdef __cplusplus extern c #endif 的作用

    #ifdef __cplusplus // C++编译环境中才会定义__cplusplus (plus就是"+"的意思) extern "C" { // 告诉编 ...

  3. “#ifdef __cplusplus extern "C" { #endif”的定义

    平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #end ...

  4. “#ifdef __cplusplus extern "C" { #endif”的定义-----C和C++的互相调用

    "#ifdef __cplusplus extern "C" { #endif"的定义 看一些程序的时候老是有 "#ifdef __cplusplus ...

  5. #ifdef __cplusplus extern "C" { #endif”的定义

      平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #e ...

  6. #ifdef __cplusplus extern "C" { #endif”的定义的含义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  7. #ifdef __cplusplus extern "C" { #endif 的解释

    好多程序中都会遇到下列代码段: #ifdef __cplusplus extern "C" { #endif /****************** C语法代码段 ******** ...

  8. 备忘录:“#ifdef __cplusplus extern "C" { #endif”的定义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  9. undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif

    最近在弄一个进程间通信,原始测试demon用c语言写的,经过测试ok,然后把接口封装起来了一个send,一个recv. 使用的时候send端是在一个c语言写的http服务端使用,编译ok没有报错,但是 ...

随机推荐

  1. poj_3436 网络最大流

    题目大意 生产电脑的工厂将一台电脑分成P个部件来进行流水线生产组装,有N个生产车间,每个车间可以将一个半成品电脑添加某些部件,使之成为另一个半成品电脑或者成为一台完好的电脑,且每个车间有一个效率,即在 ...

  2. redis基础----->redis的基本使用(一)

    这里我们就在虚拟机中安装redis,并且使用java和python实现简单的操作.深情是我承担不起的重担,情话只是偶尔兑现的谎言. redis的使用 下载地址:https://redis.io/.安装 ...

  3. 【MySQL案例】error.log的Warning:If a crash happens thisconfiguration does not guarantee that the relay lo(转)

    标签: 1.1.1. If a crash happens thisconfiguration does not guarantee that the relay log info will be c ...

  4. pythonMD5加密

    #MD5加密def md5_key(arg): hash = hashlib.md5() hash.update(arg) return hash.hexdigest()

  5. 170703、springboot编程之模板使用(thymeleaf、freemarker)

    官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度! thymeleaf的使用 1.在pom中增加thymeleaf支持 <dependency> <g ...

  6. sqlserver字符串多行合并为一行

    --创建测试表 CREATE TABLE [dbo].[TestRows2Columns]( [Id] [,) NOT NULL, [UserName] [nvarchar]() NULL, [Sub ...

  7. Gitlab备份和恢复操作

    参考:https://www.cnblogs.com/kevingrace/p/7821529.html 一,设置开启备份以及备份路径 /etc/gitlab/gitlab.rb gitlab_rai ...

  8. Drools规则引擎

    一.简介 Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules ...

  9. HTTPS握手

    作用 内容加密 建立一个信息安全通道,来保证数据传输的安全: 身份认证 确认网站的真实性 数据完整性 防止内容被第三方冒充或者篡改 https的采用了对称加密和非对称加密.握手过程中采用非对称加密,得 ...

  10. 服务器和客户端的交互方式(Socket,http协议)和各自特点适用范围

    1 数据传输方式 1.1  Socket传输的定义和其特点 所谓socket通常也称作"套接字",实现服务器和客户端之间的物理连接,并进行数据传输,主要有UDP和TCP两个协议.S ...