外部“C” (C 链接)默认

发布于 2024-09-26 00:48:42 字数 387 浏览 2 评论 0原文

问题

GCC、MSVC 或 Clang 或某些组合是否支持将链接设置为默认为 C?

背景

我有一个大型混合 C/C++ 项目,虽然使用 C 链接导出 C++ 源中的符号很容易且符合逻辑,但这些相同的源假设项目其余部分中的内容位于 C++ 链接下。

当前情况要求我使用 extern "C++" 显式包装 C 源代码使用的 C++ 源代码中定义的所有内容以及 C++ 源代码使用的 C 源代码中的所有内容。

最重要的是,我不能将 extern "C" 放在整个源文件或头文件中,因为实际的 C++ 内容会抱怨。 (例如来自 #include 或我定义的模板。)

Question

Do GCC, MSVC, or Clang, or some combination support setting linkage to default to C?

Background

I have a large mixed C/C++ project, and while it's easy and logical to export symbols in the C++ sources with C linkage, those same sources are assuming the stuff in the rest of the project are under C++ linkage.

The current situation requires me to explicitly wrap anything the C sources use that is defined in the C++ sources and everything the C++ sources use from the C sources with extern "C++".

To top things off, I can't put extern "C" around entire source or header files, as the actual C++ stuff will then complain. (Such as from #include <memory> or templates I've defined.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-10-03 00:48:42

头文件中的标准模式是:

#ifdef __cplusplus

// C++ stuff

extern "C" {
#endif

// C/C++ stuff

#ifdef __cplusplus
}
#endif

我不确定您是否还有其他选项。 C/C++ 内容必须在各处使用 C 链接进行声明。 C++ 特定的内容必须在各处使用 C++ 链接进行声明。

The standard pattern in a header file is:

#ifdef __cplusplus

// C++ stuff

extern "C" {
#endif

// C/C++ stuff

#ifdef __cplusplus
}
#endif

I'm not sure you've got any other options. The C/C++ stuff must be declared with C linkage everywhere. The C++-specific stuff must be declared with C++ linkage everywhere.

忆沫 2024-10-03 00:48:42

默认情况下,“C”链接仅对 C 源有意义,对 C++ 源没有意义,反之亦然。 “C”链接通常意味着名称/符号不会被破坏。 “C”链接的表达能力不足以用于 C++ 源,例如重载函数。

"C" linkage by default makes only sense for C sources, not for C++ sources, and vice versa. "C" linkage usually implies that names/symbols will not be mangled. "C" linkage is not expressive enough to be usable for C++ sources, e.g. for overloaded functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文