lento wrote:
>
two qusetion:
> how to
difine the globle variable in more than one c files.now ,I define them
in one head file,including the head file in all c files.
> complying error: multidefine link error .
this is basic C: #include means literally what it means. it includes the text of the header file into the source file. now, declaring a variable in a header file really defines it ! if you include your header 2 times, you define your variable 2 times, with the same name, thus a symbol defined multiple times.
so, in the header, declare your variable as extern. in one of the source file (only in one, the best being the file first initializing the variable) define your variable and initialize it, in the other file just use your variable. beware not using it while it is still uninitialized...
e.g.:
in variable.h:
extern int variable;
in variableinit.c:
#include "variable.h"
int variable = 0;
void init()
{
variable = ....; // initialize your variable
}
in variableuse.c:
#include "variable.h"
void use()
{
printf( "%i\n", variable );
}