C++外部/多重定义
我正在尝试使用 externs 与 C++ 中的 Ada 进行交互。这两种实现有什么区别?
实现 A
namespace Ada
{
extern "C"
{
int getNumber();
int index;
int value;
}
}
实现 B
namespace Ada
{
extern "C"
{
int getNumber();
}
extern "C" int index;
extern "C" int value;
}
两个实现都编译得很好。但是 Impl-A 无法链接,我收到 index 和 value 的多重定义错误。我只是想了解其中的差异。
I am trying to interface to Ada in C++ using externs. What is the difference between these two implementations?
Implementation A
namespace Ada
{
extern "C"
{
int getNumber();
int index;
int value;
}
}
Implementation B
namespace Ada
{
extern "C"
{
int getNumber();
}
extern "C" int index;
extern "C" int value;
}
Both implementations compile just fine. But Impl-A fails to link, I get a multiple definition error for index and value. I'm just trying to understand the differences.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
extern“C”仅传达用于 extern“C”块内的代码的链接约定。该块中的任何内容都将被链接起来,就好像它是纯 c 一样。令人困惑的是, extern int 完全不同。这意味着你保证在某处有一个实际的 int 命名索引和一个实际的 int 命名值,但在这里找不到它们。在您的实现-A 中,整数实际上不是第二种意义上的外部 - 外部“C”仅意味着它们提供严格的 c 链接约定。
相同的关键字但完全不同的用途,这是不幸的,因为它会导致像这样的奇怪问题。混合它们是合法的(显然),但它们的行为方式并不像它们的名字所暗示的那样。
编辑
请参阅 Charle 的回复,了解 C++ 标准中定义的外部怪异的真正定义。
extern "C" only conveys the linking conventions to use for the code within the extern "C" block. Anything in that block will be linked against as if it were pure c. Confusingly, extern int is totally different. It means that you promise there is an actual int named index and an actual int named value somewhere, but they cannot be found here. In your implementation-A the ints are actually not extern in the second sense - the extern "C" only implies that they provide a strict c linking convention.
Same keyword but totally different uses, which is unfortunate since it leads to weird issues like this. Mixing them is legal (obviously), but they don't behave together the way that their name implies.
EDIT
See Charle's response for the true definition of the extern weirdness as defined in the C++ standard.
应用于大括号内的声明序列的链接说明符(即
extern "C"
或extern "C++"
)对于是否所包含的声明是否是定义,但是应用于单个声明的链接说明符被视为extern
说明符,以便确定声明是否也是定义。 (C++03 的 7.5 第 7 段)因此:
A linkage-specifier (i.e.
extern "C"
orextern "C++"
) applied to a brace enclosed sequence of declarations has no effect on whether the enclosed declarations are definitions or not, however a linkage-specifier applied to a single declaration is treated as anextern
specifier for the purposes of determining whether a declaration is also a definition. (7.5 para 7 of C++03)So:
我不知道为什么第二个有效,但你想要,
因为你只想声明
index
和value
,而不是定义它们。 (请参阅这个答案的差异。)I'm not sure why the second works, but you want
because you only want to declare
index
andvalue
, not define them. (See this answer for the difference.)