I am declaring an array of structures, and want to define the first array component in one file and second array component in another file. The following is an example.
header.h
struct st1 {
int a;
int b;
}
file1.c
struct st1 structure1[2];
I want to use initialize structure1 components from different files as below
file2.c
extern struct st1 structure1[0] = { 10, 100 };
file3.c
extern struct st1 structure1[1] = { 200, 500 };
Also please note that in file2.c
and file3.c
, the definitions are not inside functions.
If I try to compile, linker throws errors for multiple definition.
After searching with Google, I got to know that the definition of extern
can happen only once.
Can we accomplish such kind of definition of extern
array in different source code files?
Some more information: file2.c and file3.c have constants, which I will be using in file1.c. My current implementation is I am using init() functions in file2.c and file3.c. file1.c uses initialized values to decide the course of execution. Since file2.c and file3.c are exporting constants, my intention is to avoid 2 additional init calls from file1.c.