当包含C库时,什么时候应该在“extern”C“中包含 [英] When should you wrap include in `extern "C"` when including a C Library

查看:151
本文介绍了当包含C库时,什么时候应该在“extern”C“中包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解,当您在 c ++ 项目中包含 c 标题时,必须用 externC,因为 c ++ c 功能。 c 将使用名称来标识函数, c ++ 必须使用名称和参数来满足函数重载。

I understand that when you include a c header in your c++ project you must wrap it with extern "C" because c++ and c have two different ways of identifying function. c will use the name to identify a function and c++ must use the name and the parameters to satisfy function overloading.

我不明白为什么有 c 标题,不需要包装 externC like windows.h

What I don't understand is why are there are c headers that don't require to be wrapped in extern "C" like windows.h??

推荐答案

通常,在 externC中包装C头不是一个好主意。标题可能包括在您执行此操作时断开的其他文件。在C ++中使用的设计的C标头将适当处理 externC,而无需执行任何操作。典型代码:

In general, wrapping a C header in extern "C" is not a good idea. The header might include other files that break when you do this. A C header that is designed to be used in C++ will handle extern "C" appropriately, without you having to do anything. Typical code:

#ifndef MY_HEADER_INCLUDE_GUARD
#define MY_HEADER_INCLUDE_GUARD

#ifdef __cplusplus
extern "C" {
#endif

/* C callable stuff goes here */

#ifdef __cplusplus
}
#endif

#endif /* MY_HEADER_INCLUDE_GUARD */

这篇关于当包含C库时,什么时候应该在“extern”C“中包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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