The extern
specifier is used in variable and function declarations (except class members or function parameters). It specifies external binding and does not technically affect storage duration, but it cannot be used in a definition of an automatic storage duration object, so all extern
objects have static or threaded durations.
This is useful when you have global variables and declare their existence in a header so that every source file that includes the header knows about it, but you only need to “set” it once in one of your source files .
Example, Suppose you have 3 files and you will link two files:
vim global.hpp
vim global.cpp
vim main.cpp
After compiling the output will be: 9.36, but available for all files.
Another way to use the extern keyword is to compile C++ projects into C, for example.
Suppose you are writing a plugin/extension for a program written C, but you are using C++, so you can change the context of your code to C .
Example:
If a program is written in C it will work with this code, even using classes, constructors, destructors, namespace,…
That’s all for today, small daily doses that will always keep us in tune with C++!