外部链接是什么意思 [英] What does external linkage mean
问题描述
请考虑以下代码:
#include <stdio.h>
namespace EnclosingNmspc
{
namespace Nmspc
{
extern int a;//This a and the a defined above denote the same entity
int a=5;
}
}
extern int a;
int main()
{
printf("%d\n",a);
}
有来自3.5 / 2的报价:
There is the quote from 3.5/2:
当名称具有外部链接时,其表示的实体可以是由其他翻译单位范围的名称引用的
或从
其他范围
When a name has external linkage , the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.
我不明白为什么这条规则在我的案例中不起作用?我有未定义的引用
链接器错误。
I dont understand why this rule doesn't work in my case? I have undefined reference
linker error.
推荐答案
Your question is already answered there canonically.
您在其他编译单元中缺少对 :: a
的定义。
You've been missing to have a definition for ::a
in a different compilation unit.
int a = 5; 实际上定义
extern int a;
在同一范围内。但这不能通过
int a=5;
actually defines extern int a;
in the same scope. But that's not accessed with
printf("%d\n",a);
。要检查您的命名空间中的内容,请尝试
in your main program. To check for the stuff from your namespace try
printf("%d\n",EnclosingNmspc::Nmspc::a);
这篇关于外部链接是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!