B
Bo Yang
Hi,
Look the following c program, why there is a redefinition of "t"?
[struggleyb@cache duplite-delcare]$ cat inc.h
#ifndef INC
#define INC
struct test{
int ok;
};
extern struct test t;
#endif
[struggleyb@cache duplite-delcare]$ cat test1.c
#include <stdio.h>
#include "inc.h"
struct test t;
int main(int argc, char ** argv)
{
ttt();
printf("t.ok in test1: %d\n", t.ok);
test3();
return 0;
}
[struggleyb@cache duplite-delcare]$ cat test2.c
#include <stdio.h>
#include "inc.h"
struct test t;
void ttt()
{
t.ok=1000;
printf("t.ok in test2: %d\n", t.ok);
}
[struggleyb@cache duplite-delcare]$ cat test3.c
#include <stdio.h>
#include "inc.h"
void test3()
{
printf("t.ok in test3: %d\n", t.ok);
}
[struggleyb@cache duplite-delcare]$ gcc34 -o test test3.c test2.c
test1.c
[struggleyb@cache duplite-delcare]$ ./test
t.ok in test2: 1000
t.ok in test1: 1000
t.ok in test3: 1000
At least, I define two copy of t, why there is no redefinition error?
Thanks!
Regards!
Bo
Look the following c program, why there is a redefinition of "t"?
[struggleyb@cache duplite-delcare]$ cat inc.h
#ifndef INC
#define INC
struct test{
int ok;
};
extern struct test t;
#endif
[struggleyb@cache duplite-delcare]$ cat test1.c
#include <stdio.h>
#include "inc.h"
struct test t;
int main(int argc, char ** argv)
{
ttt();
printf("t.ok in test1: %d\n", t.ok);
test3();
return 0;
}
[struggleyb@cache duplite-delcare]$ cat test2.c
#include <stdio.h>
#include "inc.h"
struct test t;
void ttt()
{
t.ok=1000;
printf("t.ok in test2: %d\n", t.ok);
}
[struggleyb@cache duplite-delcare]$ cat test3.c
#include <stdio.h>
#include "inc.h"
void test3()
{
printf("t.ok in test3: %d\n", t.ok);
}
[struggleyb@cache duplite-delcare]$ gcc34 -o test test3.c test2.c
test1.c
[struggleyb@cache duplite-delcare]$ ./test
t.ok in test2: 1000
t.ok in test1: 1000
t.ok in test3: 1000
At least, I define two copy of t, why there is no redefinition error?
Thanks!
Regards!
Bo