What does external linkage mean [duplicate]

吃可爱长大的小学妹 提交于 2019-12-12 01:25:49

问题


Consider the following code:

#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);
}

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.


回答1:


Your question is already answered there canonically.

You've been missing to have a definition for ::a in a different compilation unit.

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);


来源:https://stackoverflow.com/questions/23846057/what-does-external-linkage-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!