导出 C 函数以用于 C 或 C++ 语言可执行文件
11/04/2016
本文内容
如果使用 C 编写的 DLL 中的函数需要从 C 语言或 C++ 语言模块进行访问,则应使用 __cplusplus 预处理器宏来确定所编译的语言,然后使用 C 链接声明这些函数(如果从 C++ 语言模块进行使用)。 如果使用此方法并为 DLL 提供头文件,则 C 和 C++ 用户可以在不进行任何更改的情况下使用这些函数。
下面的代码演示 C 和 C++ 客户端应用程序可以使用的头文件:
// 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++ 源文件中执行以下操作,以防止编译器修饰 C 函数名称:
extern "C" {
#include "MyCHeader.h"
}
你希望做什么?
你想进一步了解什么?
请参阅