tags:

views:

1996

answers:

6

Hello all,

I want to understand the external linkage and internal linkage and their difference. Also I want to know any const. variable in internally link by default unless otherwise stated as extern. What does this mean.

+1  A: 

See some explanation on linkage here.

Cătălin Pitiș
Refers to C, not C++. The linkage rules are somewhat different between the two.
anon
+5  A: 

When you write an implementation file (.cpp or .cxx or something else) your compiler generates a translation unit. This is somehow the object file from you implementation file plus all the headers you #included 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, accessable through the whole program, which is the combination of all translation units (or object files).

dudewat
I'd upvote this except for one glitch: A translation unit is not "somehow the object file", it's the source code from which the compiler _creates_ the object file.
sbi
+8  A: 

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 and 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 using static for internal linkage is deprecated since it can't be used on classes and you should use anonymous namespaces which although they have external linkage are unreachable from other translation units.

namespace {
   int i; // eternal linkage but unreachable from other translation units.
   class invisible_to_others { };
}
Motti
The implementation of the "export" keyword highlighted a difference between a function declared 'static' and a function declared in the unnamed namespace. To summarise as best I can, an function template declared with the export keyword in one translation unit can refer to a function defined in an unnamed namespace of a different translation unit as a result of 2-phase lookup. (http://www.ddj.com/showArticle.jhtml?articleID=184401584)
Richard Corden
What if I do following:1.cpp<code>const int ci;</code>2.cpp<code>extern const int ci;</code>
Rajendra Kumar Uppal
A: 

Here is a good explanation.

Rajendra Kumar Uppal
+2  A: 
  • 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);
    //...
}
  1. 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).
  2. max is defined as an integer constant. The default linkage for constants is internal. So that max can be accessed in other files. Its linkage is made external with the keyword extern.
  3. 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;
}
  1. max is declared to have external linkage. A matching definition for max(with external linkage) must appear in some file. (As in 1.cpp)
  2. n is declared to have external linkage.
  3. z is defined as a global variable with internal linkage.
  4. 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!

Rajendra Kumar Uppal
+1  A: 

In terms of 'C' (Because static keyword has different meaning between 'C' & 'C++')

Lets talk about different scope in 'C'

SCOPE: It is basically how long can I see something and how far.

  1. Local variable : Scope is only inside a function. It resides in the STACK area of RAM. Which means that every time a function gets called all the variables that are the part of that function, including function arguments are freshly created and are destroyed once the control goes out of the function. (Because the stack is flushed every time function returns)

  2. Static variable: Scope of this is for a file. It is accessible every where in the file
    in which it is declared. It resides in the DATA segment of RAM. Since this can only be accessed inside a file and hence INTERNAL linkage. Any
    other files cannot see this variable. In fact STATIC keyword is the only way in which we can introduce some level of data or function
    hiding in 'C'

  3. Global variable: Scope of this is for an entire application. It is accessible form every where of the application. Global variables also resides in DATA segment Since it can be accessed every where in the application and hence EXTERNAL Linkage

By default all functions are global. In case, if you need to hide some functions in a file from outside, you can prefix the static keyword to the function. :-)

Libin Jose