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.
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.
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).
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 class
es 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 { };
}
Consider following example:
void f(int i);
extern const int max = 10;
int n = 0;
int main()
{
int a;
//...
f(a);
//...
f(a);
//...
}
#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;
}
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!
Lets talk about different scope in 'C'
SCOPE: It is basically how long can I see something and how far.
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)
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'
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. :-)