C + + series: extern

Source: Internet
Author: User

extern Action 1: declaring an external variable
Modern compilers generally take the form of compilation by file, so at compile time, the global variables defined in each file are
Transparent to each other, that is, at compile time, the visible domain of the global variable is limited to the inside of the file.

Example 1:
Create a project that contains A.cpp and B.cpp two simple C + + source files:
A.cpp:
int IRI;
int main ()
{
//.....
}

B.cpp
int IRI;

GCC a.cpp-c
GCC b.cpp-c
Compile the A.O, b.o are no problem.
But when GCC a.o b.o-o test,
MAIN.O: (. bss+0x0): Multiple definition of ' IRI '
B.O: (. bss+0x0): first defined here
Error: Redefined.
(But there was a very unexpected discovery: when the same code, using the A.C B.C, and using GCC compile, it is not reported that the redefinition of the error, very do not understand what is going on. )
That is to say, in the compilation phase, the global variables defined in each file are transparent to each other, and when compiling a, I is not aware of B, and similarly, when compiling B, I is not aware of the definition of a.
However, at the link stage, the contents of each file are "integrated", so if the names of the global variables defined in some files are the same, then an error occurs at this point, which is the duplicate definition error indicated above. Therefore, the global variable names defined in each file are not identical.

However, if you define IRI in B.cpp in the following way: use it directly in the A.cpp. It is not possible to compile A.cpp.
A.cpp
int main ()
{
iri=64;
}

B.cpp
int IRI;

GCC a.cpp-c
Was wasn't declared in this scope.

Because the compiler compiles as a file, it is not known that IRI is defined in B.cpp when compiling A.cpp.
That is: the visibility of the global variables defined in the file extends to the entire program after the link is complete, and in the compilation phase, their visibility is still limited to their own files.
The solution is as follows:
The compiler is not looking long enough, and the compiler is not aware that a variable symbol, although not defined in this file, may be defined in other files.
Although the compiler is not visionary, we can give it hints to help it solve the problem above. This is the role of extern.
The principle of extern is simply to tell the compiler: "You are compiling a file that has an identifier that is not defined in this file, but it is a global variable defined in another file, you have to release it!" ”
A.cpp:
extern int IRI;
int main ()
{
IRI = 64;
//.....
}

B.cpp
int IRI;
This will allow the compilation to pass.
extern int IRI; No space is allocated, only the compiler is notified, and the IRI is defined in other files.


extern action 2: calling C-compiled functions in C + + files
C-Compile and C + + compilation
new features such as overloads are added in relation to c,c++. So there is a big difference between the global variables and the naming of function names after compilation.
int A;
int Functiona ();
for C-way compilation:
int a;=> _a
int Functiona (); = = _functiona
for C + + compile:
int A; =>[email  protected]@a
int Functiona (); = = [email protected] @functionA
can be seen, because overloading is supported, so C + + is compiled, The generated global variable names and function names are much more complex. is different from the C-way compilation with an underscore. The
then has the following conditions:
Example 2:c++ calls the global variable defined by C + +
//a.cpp:
extern int IRI;
int main ()
{
IRI = +;
//.....
}
//b.cpp
int iRI;
GCC a.cpp-c
gcc b.cpp-c
gcc a.o b.o-o Test
It's okay to compile the link.

Example 3:c++ calling a global variable defined by C
A.cpp:
extern int IRI;
int main ()
{
IRI = 64;
//.....
}
B.c
int IRI;
No problem at compile time,
GCC a.cpp-c
GCC b.c-c
But when linked, GCC b.o a.o-o test
It will be reported that IRI is undefined. Why is it?
Because GCC sees A.cpp, it compiles using C + +, and sees B.C, it compiles using C mode.
So the Iri=>[email Protected]_iri in the A.cpp;
and B.C in Iri=〉_iri;
So in the link, A.cpp want to find [email Protected]_iri, of course, can not find. So you need to tell the compiler that IRI is compiled using C.
A.cpp:
extern "C"
{
int IRI;
}
int main ()
{IRI = 64;
//.....
}
B.c
int IRI;
This way, when compiling A.cpp, the compiler knows that IRI is compiled in C. You will use _iri. So the _iri provided by B.C can be found by A.cpp.

Example 4:c++ calling the function defined by C
A.cpp
extern int Functiona ();

int main ()
{
Functiona ();
}

B.c
int Functiona ()
{
//....
}
GCC a.cpp-c
GCC b.c-c
There's no problem. But the same, GCC a.o b.o-o test
The error, can not find Functiona ();
This is because GCC compiles A.cpp as C + +, and B.C is C-mode.
So Functiona in B.C: _functiona. In A.cpp: [Email Protected]_functiona
So when the link A.cpp can't find [email protected]_function.
Then you need to inform the compiler that Functiona () is a C-style compiler named.
A.cpp
extern "C"
{
int Functiona ();
}
int main ()
{
Functiona ();
}

B.c
int Functiona ()
{
//....
}
As a result, the compile link can be passed.
Summarize:
extern "C"
{
Functiona ();
}//is not just a statement, it also states: This function should be compiled in C mode. So there is no need to extern again.
extern "C"
{
extern Functiona ();
}//It doesn't make much sense to do so.

C + + series: extern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.