First you need to figure out what the definition is and what the Declaration is (declaration).
Declaration of a function function:
int myfunc (int a,int b);
Defined:
int myfunc (int a,int b) {return a+b;}
In. h Although you can declare and define functions, this. h can only be used once, otherwise a redefinition error will occur:
Error LNK2005 _myfunc already defined in Xxx.obj
so try not to define functions in. h .
Second, the variable variable declaration:
extern int A;
If the declaration contains an initialization expression, it is treated as defined , even if it is preceded by extern. The extern declaration can be initialized only if it is outside the function. Therefore, do not initialize the extern declaration .
Defined:
int a;//a is an integer external variable
The meaning of the external variable definition and the external variable declaration is different. an external variable can be defined only once , its position is outside of all functions, and the declaration of an external variable in the same file can be multiple times, its position can be within the function, or it can be outside the function.
The following are good program design styles and conventions:
The function declaration in the header file (. h), and the definition of the function in the source file (. c);
The declaration of the variable is uniformly put to the head file;
Reference: http://www.cnblogs.com/magicsoar/p/3702365.html
Http://c.biancheng.net/cpp/biancheng/view/143.html
http://my.oschina.net/xinxingegeya/blog/232758
Resolve problem with already defined in. obj (difference in definition/declaration)