Defining an external array from different files

I declare an array of structures and want to define the first component of the array in one file and the second component of the array 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 the initialization of structure1 components from different files as shown below

file2.c

extern struct st1 structure1[0] = { 10, 100 };

file3.c

extern struct st1 structure1[1] = { 200, 500 };

Also note that in file2.cand file3.cdefinitions are not part of functions.

If I try to compile, the linker throws errors for multiple detection. After searching on Google, I found out that a definition externcan only happen once. Can we do this array definition externin different source code files?

: file2.c file3.c , file1.c. - init() file2.c file3.c. file1.c . file2.c file3.c , 2 init file1.c.

+4
3

, , , , .

, , :

////// header.h //////
struct st1 {
    int a;
    int b;
};

extern struct st1 structure1[2];

extern void init2();
extern void init3();

, ; :

////// file1.c //////
#include "header.h"

struct st1 structure1[2];

, ; , , .

////// file2.c //////
#include "header.h"

void init2()
{
    structure1[0] = (struct st1){10, 100};
}

////// file3.c //////    
#include "header.h"

void init3()
{
    structure1[1] = (struct st1){200, 500};
}

, . , , :

////// main.c //////
#include "header.h"
#include <stdio.h>

int main(void)
{
    init2();
    init3();

    printf("s[0].a = %d, s[1].a = %d\n", structure1[0].a, structure1[1].a);

    return 0;
}

:

cc -o file file1.c file2.c file3.c main.c
+3

file2.c file3.c , , ; linker . , , file1.c header.h

, , file2.c file3.c . , ( Linux) file1.c file2.c proga,

gcc -c -Wall -g file1.c
gcc -c -Wall -g file2.c
gcc file1.o file2.o -o proga

( Makefile), file1.c file3.c progb

gcc -c -Wall -g file1.c
gcc -c -Wall -g file3.c
gcc file1.o file3.o -o progb

, file1.c, file2.c, file3.c #include "header.h". , file1.c ( make ), file1.o proga progb

, , header.h

// in header.h
extern struct st1 structure1[];

structure1 ( ). , structure1[0] file2.c structure1[1] file3.c ( , fill_first fill_second, , , , main)

( file2.o proga, file3.o progb), file2.c file3.c ( structure1, !) ,

 // in file2.c
 struct st1 structure1[2] = { {1,2}, {3,4} };

 // in file3.c
 struct st1 structure1[3] = { {0,1}, {2,3}, {3,4} };

, init.c ( (, structure1)), (, #if #ifdef) proga progb, , , - , init-proga.o init-progb.o,...

+2

Your file header.hhas

struct st1 {
    int a;
    int b;
};

extern struct st1 structure1[2];  /* Have your declaration here */

In one of your files .c, let's say file1.cyou can define your structure:

struct st1 structure1[2];  /* Define your structure here */

void func1()
{
  structure[0].a = 10;
  structure[0].b = 100;
}

And in file2.c:

#include "header.h"  /* Make the declaration of your structure visible here */

void func2()
{
  structure[1].a = 200;
  structure[1].b = 500;
}

Of course, the structure will not be fully initialized until both functions are called.

+1
source

All Articles