这背后的理论是什么:#ifdef __cplusplus extern"C" { [英] What's the theory behind this: #ifdef __cplusplus extern "C" {
本文介绍了这背后的理论是什么:#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++
功能,在语言).
该机制允许C
和C++
编译器使用相同的标头(__cpluspls
宏仅由C++
定义)并以这种方式工作:
C
编译器在构建DLL
以及在应用程序中包含DLL
标头时都忽略extern "C"
(未定义__cplusplus
)伪指令.
C++
编译器,根据extern "C"
(已定义__cplusplus
)伪指令:
- 在构建
DLL
时产生标准的C DLL
(即函数使用C
语言处理方案).- 在标头中将库视为
C DLL
包含在应用程序中(您知道C++
编译器可以与C
库链接).
That typically enclosesDLL
''s exported functions.
You should know,C
Language andC++
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, aC++
feature, not available inC
language).
The mechanism allows the same header to be used by both theC
andC++
compiler (__cpluspls
macro is defined only by theC++
one) and works this way:
TheC
compiler, ignores theextern "C"
(__cplusplus
is not defined) directive both when building theDLL
and when including theDLL
header inside an application.
TheC++
compiler, according with theextern "C"
(__cplusplus
is defined) directive:
- Produces a standard
C DLL
(i.e. the function use theC
language mangling scheme) when building thDLL
.- consider the library as a
C DLL
when the header is included in a application (you knowC++
compiler is able to link withC
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屋!
查看全文