Evo bukvalno ovako sam napravio fajlove kao test primer i isto mi se dešava.
Code:
// heder.h
#ifndef HEDER_H_
#define HEDER_H_
struct X {
int a;
int b;
};
struct X nizX[] = {
[0] = {
.a = 1,
.b = 2,
},
[1] = {
.a = 3,
.b = 4,
},
[2] = {
.a = 5,
.b = 6,
},
};
#endif /* HEDER_H_ */
Code:
// prvi.c
#include "heder.h"
void t(int a, int b) {
nizX[0].a = a;
nizX[0].b = b;
}
Code:
// drugi.c
#include "heder.h"
#include <stdio.h>
int main() {
nizX[1].a = 5;
nizX[2].b = 6;
int i;
for (i = 0; i < sizeof(nizX)/sizeof(struct X); i++) {
printf("%d := %d %d", i, nizX[i].a, nizX[i].b);
}
return 0;
}
Eclipse C projekat sa mingw kompajelrom.
Code:
Description Resource Path Location Type
first defined here prvi.c /struktura/src C/C++ Problem
multiple definition of `nizX' drugi.c /struktura/src C/C++ Problem
Code:
**** Build of configuration Debug for project struktura ****
**** Internal Builder is used for build ****
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o src\drugi.o ..\src\drugi.c
gcc -o struktura.exe src\prvi.o src\drugi.o
src\drugi.o:drugi.c:(.data+0x0): multiple definition of `nizX'
src\prvi.o:prvi.c:(.data+0x0): first defined here
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 438 ms.
Verovatno treba negde da se stavi extern, ali ni to nisam uspeo da provalim.