A long time no contact with C language, today encountered a statement and definition of the problem, and then found the following explanation:
The Declaration (Declaration) specifies the identifier of a variable that describes the type of the variable, whether it is a type or an object, or a function. Declaration, used by the compiler (compiler) to identify the entity referenced by the variable name. These are the declarations:
extern int bar;
extern int g (int, int);
Double f (int, double); For function declarations, the extern keyword can be omitted.
class Foo; class declaration, which cannot be preceded by class.
A definition is an implementation or instantiation of a declaration. The connector (linker) needs it (defined) to refer to the memory entity. The definition corresponding to the above statement is as follows:
int bar;
int g (int lhs, int rhs) {return LHS*RHS;}
Double f (int i, double d) {return i+d;}
class Foo {};//foo already has its own memory, and against the above two functions, you should understand the usefulness of {}?
In any case, defining an operation can only be done once. If you forget to define some variables that you have already declared, or those that are referenced in some places, then the connector linker is unaware that these references are connected to that block of memory. Then you will report missing symbols such a mistake. If you define a variable more than once, the connector does not know which memory to connect the reference to, and then it will report duplicated symbols such an error. The above symbols actually refers to the defined variable name, which is the memory block of its identity.
Summarize
If it is just to provide the compiler with a reference identity, so that the compiler can know that there is a reference to that reference, it can be used to refer to an entity (but not the process of allocating a specific block of memory to the entity) for the declaration. If the operation is able to specify a specific piece of memory for the reference, so that the reference can only correctly correspond to a piece of memory in the link phase, the operation is defined.
The declaration is intended to allow the compiler to correctly handle references to declared variables and functions. A definition is a process of allocating memory to a variable, or a function that is specific to what it is used for.
C Language Declaration and definition