教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高等教育 >

关于“#ifdef___cplusplus”_和__extern_C

来源:网络收集 时间:2026-08-02
导读: 关于extern C 和 extern “C” far的说明 #ifdef __cplusplus extern C { #endif #ifdef __cplusplus } #endif 宏定义里面这样写有什么作用啊?extern C 是啥意思啊! //这是条件编译,分别生成c代码or c++代码, //如果定义了__cplusplus(c++的意思)就定

关于extern "C" 和 extern “C” far的说明

#ifdef __cplusplus

extern "C" {

#endif

#ifdef __cplusplus

}

#endif

宏定义里面这样写有什么作用啊?extern "C" 是啥意思啊! //这是条件编译,分别生成c代码or c++代码,

//如果定义了__cplusplus(c++的意思)就定义extern "C" ,这是兼容c函数的用法。

还有就是extern far void vectors();中 far 是什么意思啊?//far 是调用的方式,简单的说,比如代码较近,我们就可一用短指针访问,near 调用,但是如果表示代码的地址很长,就得用长指针或者间接访问,后者就是far 调用.

在CSL.H里面还会见到:

#ifdef __cplusplus

#define CSLAPI extern "C" far

#else

#define CSLAPI extern far

#endif

这里是不太明白 extern "C" far 和extern far 的区别。并且__cplusplus这个东西一般到底定义了没有?

不太明白这些东西。

在这里请教大家 多谢!

// 这个和你的编译器和工作环境有关系啊,如果你用c代码,__cplusplus就没用定义, //extern 是声明,表示这是个外部变量,定义在其他模块。

注意以下结构: #ifdef __cplusplus

extern "C"

{ #endif

#ifdef __cplusplus

}

#endif 宏定义里面这样写有什么作用啊?extern "C" 是啥意思啊? //这是条件编译,分别生成c代码or c++代码, //如果定义了__cplusplus(c++的意思)就定义extern "C" ,这是兼容c函数的用法。

还有就是extern far void vectors();中 far 是什么意思啊? //far 是调用的方式,简单的说,比如代码较近,我们就可以用短指针访问,near 调用,但是如果表示代码的地址很长,就得用长指针或者间接访问,后者就是far 调用。

在CSL.H里面还会见到:

#ifdef __cplusplus

#define CSLAPI extern "C" far

#else

#define CSLAPI extern far

#endif 这里是不太明白 extern "C" far 和extern far 的区别。并且__cplusplus这个东西一般到底定义了没有?

// 这个和你的编译器和工作环境有关系啊,如果你用c代码,__cplusplus就没用定义, //extern 是声明,表示这是个外部变量,定义在其他模块。

关于“#ifdef __cplusplus” 和 " extern "C" "

看一些程序的时候老是有

“#ifdef __cplusplus

extern "C" {

#endif”的定义,搞搞清楚是怎么回事:

Microsoft-Specific Predefined Macros

__cplusplus Defined for C++ programs only.

意思是说,如果是C++程序,就使用

extern "C"{

而这个东东,是指在下面的函数不使用的C++的名字修饰,而是用C的

The following code shows a header file which can be used by C and C++

client applications:

// MyCFuncs.h

#ifdef __cplusplus

extern "C" { //only need to export C interface if

// used by C++ source code

#endif

__declspec(dllimport ) void MyCFunc();

__declspec( dllimport ) void AnotherCFunc();

#ifdef __cplusplus

}

#endif

当我们想从C++中调用C的库时,(注,驱动是用C写的,连new、delete也不能用,郁闷)不能仅仅说明一个外部函数,因为调用C函数的编译代码和调用C++函数的编译代码是不同的。如果你仅说明一个外部函数, C++编译器假定它是

C++的函数编译成功了,但当你连接时会发现很可爱的错误。

解决的方法就是指定它为C函数:

extern "c" 函数描述

指定一群函数的话:

extern "C"{

n个函数描述

}

如果想C和C++混用的话:

#ifdef _cplusplus

extern "C"{

#endif

n个函数描述

#ifdef _cplusplus

}

#endif

extern "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++中使用C的函数,

或者在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;

}

//在C中引用C++函数

在C中引用C++语言中的函数和变量时,C++的函数或变量要声明在extern "C"{}里,但是在C语言中不能使用extern "C",否则编译出错。

//test.cpp

#include <stdio.h>

extern "C"

{

void mytest()

{

printf("mytest in .cpp file ok\n");

}

}

//main.c

void mytest();

int main()

{

mytest();

return 0;

}

//综合使用

一般我们都将函数声明放在头文件,当我们的函数有可能被C或C++使用时,我们无法确定是否要将函数声明在extern "C"里,所以,我们应该添加

#ifdef __cplusplus

extern "C"

{

#endif

//函数声明

#ifdef __cplusplus

}

#endif

如果我们注意到,很多头文件都有这样的用法,比如string.h,等等。

//test.h

#ifdef __cplusplus

#include <iostream>

using namespace std;

extern "C"

{

#endif

void mytest();

#ifdef __cplusplus

}

#endif

这样,可以将mytest()的实现放在.c或者.cpp文件中,可以在.c或者.cpp文件中include "test.h"后使用头文件里面的函数,而不会出现编译错误。

//test.c

#include "test.h"

void mytest()

{

#ifdef __cplusplus

cout<< "coutmytest extern ok " <<endl;

#else

printf("printfmytest extern ok n");

#endif

}

//main.cpp

#include "test.h"

int main()

{

mytest();

return 0;

}

extern "C" 的用意

前些天,编程序是用到了很久以前写的C程序,想把里面的函数利用起来,连接发现出现了找不到具 …… 此处隐藏:3526字,全部文档内容请下载后查看。喜欢就下载吧 ……

关于“#ifdef___cplusplus”_和__extern_C.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/128441.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)