C++/C 'extern' keyword explained

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

C++/C 'extern' keyword explained

Post by SemiconductorCat » Thu Sep 15, 2011 3:12 pm

When we built a program with multiple compilation units[ multiple source files and object files built a one single executable file] we have met situation that we need to share variables and made calls between multiple compilation units.

for a example consider the following two source files.

Code: Select all

#include <stdio.h>
extern void f1();

int main(int argc,char ** argv)
{
    f1();
    return 0;
}
file1.c

Code: Select all

#include <stdio.h>
void f1()
{
    printf ("f1() from file2\n");
    return ;
}
file2.c

Code: Select all

compile
#gcc -c file1.c file2.c
link
#gcc -o file file1.o file2.o
run
#./file
In "C" programming language the line 2 in file1.c is actually not necessary.because "C" allow us to use implicit declaration. But if you pass the '-Wall' compiler switch to the compiler you'll get a
warning message like.

Code: Select all

file1.c: In function 'main':
file1.c:5: warning: implicit declaration of function 'f1'
However in C++ , line 2 , at lest without 'extern'

Code: Select all

void f1();
is necessary, because C++ doesn't allow implicit declarations , because it's a strict language.
However you still can omit 'extern' prefix keyword.

so for calling functions which are on other compilation units [which are declared in other source files and will be separately compiled and will built a '.o' file], it's not necessary to use 'extern' keyword.


So How 'extern' keyword is useful? why it exists?
As you already seen , to call external function you don't need to use 'extern' prefix. But when you referencing to a variable that exists outside your file scope without redefine it,you need to use 'extern' keyword.Such as like this.

Code: Select all

extern int error;
which means 'error' is definied somewhere else in the code, but if you type

Code: Select all

int error;
it means error in declared and defined in the current file scope.

So let's take a example. Review following two source files.

Code: Select all

#include <stdio.h>
extern int error;

int main(int argc,char **argv)
{
    printf("error code is :%i\n",error);
    return 0;
}
main.c

Code: Select all

int error =3;
error.c

... There are other uses of 'extern' keyword such as 'extern "C"' to call older C library functions within C++. And to call routines that are written in assembly language.



Good Clear Coding Style.
Although you don't need to explicitly use 'extern' as a prefix when you want to call a external function, ignoring it is not a good coding practice. believe me no industry expert is ignoring that, because you're writing code to human to read , lastly for the compiler. So I recommend you to explicitly put that prefix 'extern' where appropriate.

--Happy Coding--
--sandun--
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: C++/C 'extern' keyword explained

Post by Saman » Thu Sep 15, 2011 5:59 pm

Very nice explanation.

I would like to add two points.
  • It is good to use CPP as the extension for C++ files
  • It would be better to use C++ in OOP style (that's why C++ is made for) where it removes the need of 'extern' completely.
Post Reply

Return to “C/C++ Programming”