C++ extern

In C++ programming language, extern is useful when you have a large project that contains a lot of files. For a large project, it is not a good practice to place all code in a single file because compiler works slowly. You may not want a compiler to recompile some parts of the code for saving time reason. However, each file should know global variables that are shared a cross the project. I mean each file uses the same global variables. In fact, the global variables are defined at once in a file. How can you let other files know those variables? The solution is to use the extern keyword. The extern allows you to redeclare global variables in each file. Thus, compiler can know those variables. I would like to clarify between declaration and definition. Declaration means you let the compiler know the variable are but there are no storage for them. When talking about definition, the storage are allocated for those variables.

Now open Dev-C++, create a new project, copy and paste the File1.cpp to the code editor and compile it by pressing Ctrl+F9. The editor will prompt you to save the file. Now save the file as File1.cpp.

File 1: File1.cpp
#include<cmath>

double a,b; //global variable are defined here

double calPerimeter(){
double c=sqrt(pow(a,2)+pow(b,2));
return(a+b+c);
}

double calArea(){
return(a*b*0.5);
}


If errors occur, read the messages them correct the errors and and compile it again.
When you compile it with success, the File1.o file will be created in the default directory of the project. This file will be used later when we link files together.
If there is no error, proceed to the next step. Create a new project in Dev-C++ and copy and paste
the File2.cpp in code editor and compile it. OK! you get some errors right now.

File 2: File2.cpp
#include <cstdlib>
#include <iostream>
using namespace std;
extern double a,b; //let the compiler know that a and b variables are declared elsewhere in a separated file
//so it will not redefine them
//this idea is also applicable to functions.
extern double calPerimeter();
extern double calArea();  

int main(){

   a=20,b=20;
   cout<<"Perimeter="<<calPerimeter()<<endl;
   cout<<"Area="<<calArea()<<endl;
  
   system("PAUSE");
   return 0;
}

You got errors because global variables a and b, and calPerimeter and calArea are unknown to the compiler. The compiler does not know them because File1.cpp and File2.cpp are separated. You need to link them together. In Dev-C++, to link files together, follow the steps below:
-Click Project menu and then select Project Options...
-In Project Options dialog box select Parameters tab
-In Linker box, type File1.o then click OK
Now compile File2.cpp again and run it.




Posted by: Dara | post date: 01-29-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.