Home  >  Article  >  php教程  >  Summary of how to use extern

Summary of how to use extern

高洛峰
高洛峰Original
2016-12-19 14:33:361156browse

extern

Functions defined in source file A are invisible (that is, inaccessible) in other source files. In order to call this function in source file B, an external declaration should be added to the head of B:


extern function prototype;


In this way, that function can also be called in source file B.​
Pay attention to the difference in wording here: in A it is a definition, in B it is a statement. A function can only (and must) be defined in one source file, but can be declared in multiple other source files. Definition causes storage allocation, which actually creates that entity. The declaration does not cause storage allocation. To give a crude analogy: after declaring it in source file B, it is like opening a window in B so that it can see the function in A.

#i nclude "stdafx.h"


1.extern is often used in variable declarations. You declare a global variable in the *.c file. If this global variable is to be To quote, place it in *.h and declare it with extern.
 2. If the keyword extern is included in the function declaration, it only implies that the function may be defined in other source files and has no other effect. That is, there is no difference between the following two function declarations:
  extern int f(); and int f();
 ========================= ======
 If the c/cpp file that defines the function declares the defined function in the corresponding header file, then to use these functions in other c/cpp files, you only need to include this header file.
 If you don’t want to include the header file, then declare the function in c/cpp. Generally speaking, "extern" is not used to declare functions defined in this file, and "extern" is used to declare functions defined in other files. In this way, when calling functions defined in other files in this file, there is no need to include header files
 include "* .h" to declare the function, you can use it directly after declaration. = ============================
For example:
//extRn.cpp content is as follows:

/ / extern.cpp: Defines the entry point for the console application.
   //
 
  #i nclude "stdafx.h"
  extern print(char *p);
 int main(int argc, char* argv[])
{
 char *p="hello world!";
 print(p);
  return 0;
 }
  //The content of print.cpp is as follows
  #i nclude "stdafx.h"
  #i nclude "stdio.h"
 print(char *s)
 {
 printf("The string is %s/n",s);
 }
 
 The result program can run normally and output the result. If you remove "extern", the program can still run normally.
 
  It can be seen that "extern" is dispensable in the function declaration. It is only used to indicate whether the function is defined in this file or in another file. As long as your function is declared before use, you don't need to include the header file.
 
“Unexpected end of file while looking for precompiled header directive” problem that often occurs in VC++6.0?

How to solve: "fatal error C1010: "unexpected end of file while looking for precompiled header directive" problem that often occurs in VC++6.0?

I think everyone often encounters this problem in VC6.0, How to solve it?

1. Check if there are missing ";", "}"

Such as: semicolons after classes and structures
The problems that are deeply hidden are macros and .h files, so you need to worry about them

2. Some of your definitions in the class must have been deleted. M$ defines some special constants in each class, which are in pairs, as follows:

.h:
#if !defined(AFX_CHILDFRM_H__54CA89DD_BA94_11D4_94D7_0010B503C2EA__INCLUDED_)
#define AFX_CHILDFRM_H__54CA89DD_BA94_11D4_94D7_0010B503C2EA__INCLUDED_
.......
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINFRM _H__54CA89DB_BA94_11D4_94D7_0010B503C2EA__INCLUDED_)

You can create a new A class, and then copy or add these.
3. Add #i nclude "stdafx.h" to the header

4. Add #i nclude "stdafx.h" to the first line of the CPP file. .

Or Rebuild All.

5.

(1). [Project] - [Settings] - [C/C++] - [Category]

(2). Select [Precomplied Headers]
(3). Single choice [Not Using Precomplied Headers]
(4). [OK]


If the above does not solve the problem, then please read the following content. To cause such an error, it is possible that you just added a .H and .CPP file. In this case, you should do as mentioned above.
The name contains "stdafx .h". If you want to use structural types in multiple files at the same time, you have to continue reading. You will definitely gain a lot.

The definition of type is different from the definition of type variable.
Type The definition only describes a type,
is for the compiler to see,
will not produce executable code.
Variable definition refers to the actual existence of such a piece of content in the executable file.

Because it is troublesome to write the type definition clearly in each .c,
So the type definition is generally written in .h
, and in .c, a simple writing method is used, such as struct A a;
Define variables like this ,
No need to write the entire type description again.

------------------------------------------------ --------------------------
Therefore, the struct type definition is placed in XX.h, and
XX.cpp is added with struct str st_r;
XXXXX.cpp Add #i nclude "XX.h"
Then use extern struct str st_r directly;



For more articles related to the summary of extern usage, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn