这背后的理论是什么:#ifdef __cplusplus extern"C" { [英] What's the theory behind this: #ifdef __cplusplus extern "C" {

查看:113
本文介绍了这背后的理论是什么:#ifdef __cplusplus extern"C" {的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一些c/c ++代码中满足了以下要求,但我不了解其背后的理论:

#ifdef __cplusplus
extern"C" {
#endif

我想了解它是如何工作的.

I have been meeting the following in some c/c++ codes but I don''t understand the theory behind it:

#ifdef __cplusplus
extern "C" {
#endif

I want to understand how it works. Someone teach me please!

推荐答案

通常包含DLL的导出函数.
您应该知道,C语言和C++使用一种不同的方法来实现名称处理(即更改对象-或库文件中的功能名称),更具体地说, C++ mangled (或 decorated )中包含有关函数参数类型的名称信息(这允许函数重载,C++功能,在语言).
该机制允许CC++编译器使用相同的标头(__cpluspls宏仅由C++定义)并以这种方式工作:
C编译器在构建DLL以及在应用程序中包含DLL标头时都忽略extern "C"(未定义__cplusplus)伪指令.

C++编译器,根据extern "C"(已定义__cplusplus)伪指令:
  • 在构建DLL时产生标准的C DLL(即函数使用C语言处理方案).
  • 在标头中将库视为C DLL包含在应用程序中(您知道C++编译器可以与C库链接).
That typically encloses DLL''s exported functions.
You should know, C Language and C++ one use different approach to function''s name mangling (i.e. altering the name of the funtion in object -or library- files), more specifically, C++ includes in the mangled (or decorated) name info about the function argument types (this allows, function overloads, a C++ feature, not available in C language).
The mechanism allows the same header to be used by both the C and C++ compiler ( __cpluspls macro is defined only by the C++ one) and works this way:
The C compiler, ignores the extern "C" (__cplusplus is not defined) directive both when building the DLL and when including the DLL header inside an application.

The C++ compiler, according with the extern "C" (__cplusplus is defined) directive:
  • Produces a standard C DLL (i.e. the function use the C language mangling scheme) when building th DLL.
  • consider the library as a C DLL when the header is included in a application (you know C++ compiler is able to link with C libraries).


默认情况下,C ++破坏名称: http://en.wikipedia.org/Wiki/Name_mangling [^ ]

extern "C"告诉编译器不要破坏该块中的名称.它通常用于从dll导出的功能,因此可以从外部代码更轻松地使用它们.

#ifdef _cplusplus检查适用于C ++编译器.

尼克
By default, C++ mangles names: http://en.wikipedia.org/wiki/Name_mangling[^]

extern "C" tells the compiler not to mangle the names in that block. It is normally used for functions that are exported from a dll, so they can be used more easily from external code.

The #ifdef _cplusplus check is for the C++ compiler.

Nick


标准C(非C ++)不支持此功能:
extern "C" {

因此,这里检查了我们是否在C ++下编译:
#ifdef __cplusplus
Standard C (non-C++) doesn''t support this:
extern "C" {

So here''s checked whether we are compiled under C++ or not:
#ifdef __cplusplus


这篇关于这背后的理论是什么:#ifdef __cplusplus extern"C" {的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆