What is external linkage and internal linkage?

I want to understand the external linkage and internal linkage and their difference.

I also want to know the meaning of

const variables internally link by default unless otherwise declared as extern .


When you write an implementation file ( .cpp , .cxx , etc) your compiler generates a translation unit . This is the object file from your implementation file plus all the headers you #include d in it.

Internal linkage refers to everything only in scope of a translation unit .

External linkage refers to things that exist beyond a particular translation unit. In other words, accessible through the whole program , which is the combination of all translation units (or object files).


As dudewat said external linkage means the symbol (function or global variable) is accessible throughout your program and internal linkage means that it's only accessible in one translation unit.

You can explicitly control the linkage of a symbol by using the extern and static keywords. If the linkage isn't specified then the default linkage is extern for non- const symbols and static (internal) for const symbols.

// in namespace or global scope
int i; // extern by default
const int ci; // static by default
extern const int eci; // explicitly extern
static int si; // explicitly static

// the same goes for functions (but there are no const functions)
int foo(); // extern by default
static int bar(); // explicitly static 

Note that instead of using static for internal linkage it is better to use anonymous namespaces into which you can also put class es. The linkage for anonymous namespaces has changed between C++98 and C++11 but the main thing is that they are unreachable from other translation units.

namespace {
   int i; // external linkage but unreachable from other translation units.
   class invisible_to_others { };
}

  • A global variable has external linkage by default. Its scope can be extended to files other than containing it by giving a matching extern declaration in the other file.
  • The scope of a global variable can be restricted to the file containing its declaration by prefixing the declaration with the keyword static. Such variables are said to have internal linkage.
  • Consider following example:

    1.cpp

    void f(int i);
    extern const int max = 10;
    int n = 0;
    int main()
    {
        int a;
        //...
        f(a);
        //...
        f(a);
        //...
    }
    
  • The signature of function f declares f as a function with external linkage(default). Its definition must be provided later in this file or in other translation unit (given below).
  • max is defined as an integer constant. The default linkage for constants is internal. Its linkage is changed to external with the keyword extern. So now max can be accessed in other files.
  • n is defined as an integer variable. The default linkage for variables defined outside function bodies is external.
  • 2.cpp

    #include <iostream>
    using namespace std;
    
    extern const int max;
    extern int n;
    static float z = 0.0;
    
    void f(int i)
    {
        static int nCall = 0;
        int a;
        //...
        nCall++;
        n++;
        //...
        a = max * z;
        //...
        cout << "f() called " << nCall << " times." << endl;
    }
    
  • max is declared to have external linkage. A matching definition for max(with external linkage) must appear in some file. (As in 1.cpp)
  • n is declared to have external linkage.
  • z is defined as a global variable with internal linkage.
  • The definition of nCall specifies nCall to be a variable that retains its value across calls to function f(). Unlike local variables with the default auto storage class, nCall will be initialized only once at the start of the program and not once for each invocation of f(). The storage class specifier static affects the lifetime of the local variable and not its scope.
  • NB: The keyword static plays a double role. When used in the definitions of global variables, it specifies internal linkage. When used in the definitions of the local variables, it specifies that the lifetime of the variable is going to be the duration of the program instead of being the duration of the function.

    Hope that helps!

    链接地址: http://www.djcxy.com/p/73050.html

    上一篇: 匿名命名空间优于静态?

    下一篇: 什么是外部联系和内部联系?