要包含自己的C头文件,用户必须将#include命令包在extern "C"语句中。可以用以下方法完成此操作:
① 在#include文件之前使用extern,如下例所示。
// C++ code
extern "C" {
#include "my-header1.h"
#include "my-header2.h"
}
int main()
{
// ...
return 0;
}
· 将extern "C"语句添加到头文件,如下例所示。
/* C header file */
#ifdef __cplusplus /* Insert start of extern C construct */
extern "C" {
#endif
/* Body of header file */
#ifdef __cplusplus /* Insert end of extern C construct */
} /* The C header file can now be */
#endif /* included in either C or C++ code. */