top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is internal linking and external linking in c++?

+2 votes
1,260 views
What is internal linking and external linking in c++?
posted Mar 26, 2014 by Mohammad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

When you write an implementation file (.cpp or .cxx or something else) 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, accessable through the whole program, which is the combination of all translation units (or object files).

Example: test.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 test.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.

answer Mar 26, 2014 by Amit Kumar Pandey
Similar Questions
0 votes

I am new to apache, so forgive me if this is a beginner questions.

I am trying to redirect request to apache based on the IP address. Depending on where the request originates from, the request will either have a external or internal IP address. External will be redirected to the external site, internal will be redirected to the internal site.

Request to www.site.com will be evaluated based on the IP address. Then redirected to either www.site.com/external or www.site.com/internal . I am able to accomplish this with the following.

 RewriteCond %{REMOTE_ADDR} ^1.1.1.1 
 RewriteRule ^(.*)$ www.site.com/internal [R=301,L] 
 RewriteCond %{REMOTE_ADDR} ^2.2.2.2 
 RewriteRule ^(.*)$ www.site.com/external [R=301,L] 

The problem I am running into is that this is in a loop. Every request will be evaluated and redirected. If possible, I need a way to redirect once, then let other request (www.site.com/external/a/b/c or www.site.com/internal/x/y/z )not be evaluated and then redirected

...