#ifdef __cplusplus

声明中的一种用法:

场景:cpp文件定义了C的函数,供其他的C++ 和C文件调用,则可以在.h中作如下声明:

//a.h


#ifndef  __cplusplus//C
VOID A();
#else//C++
extern "C" VOID A();
#endif

//a.cpp
extern "C" A(){;}

目的:保证了在C++中提供extern "C" VOID A();声明,  而在C中提供:VOID A();声明

而在gcc中,其产生的函数符号仅由函数名决定。

g++编译器下其规则为:“作用域+返回类型+函数名+参数列表”

----------------------------------------------------------------------

#ifdef __cplusplus是什么意思? 
 
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
 

猜你喜欢

转载自blog.csdn.net/qqyuanhao163/article/details/86307551