Переместите определения глобальной структуры в файл заголовка (.h) и просто #include
ваш заголовок в каждом файле c, которому требуется доступ к этим структурам.Любые глобальные переменные могут быть объявлены extern
и затем определены в вашем файле main.c.
Global.h
// WARNING: SHARING DATA GLOBALLY IS BAD PRACTICE
#ifndef GLOBAL_H
#define GLOBAL_H
//Any type definitions needed
typedef struct a_struct
{
int var1;
long var2;
} a_struct;
//Global data that will be defined in main.c
extern a_struct GlobalStruct;
extern int GlobalCounter;
#endif
main.c
#include "Global.h"
#include "other.h"
#include <stdio.h>
int GlobalCounter;
a_struct GlobalStruct;
int main(int argc, char *argv[])
{
GlobalCounter = 5;
GlobalStruct.var1 = 10;
GlobalStruct.var2 = 6;
printf("Counter: %d\nStruct [ var1: %d var2: %ld ]\n",
GlobalCounter, GlobalStruct.var1, GlobalStruct.var2);
do_other_stuff();
printf("Counter: %d\nStruct [ var1: %d var2: %ld ]\n",
GlobalCounter, GlobalStruct.var1, GlobalStruct.var2);
return 0;
}
other.h
#ifndef OTHER_H
#define OTHER_H
void do_other_stuff(void);
#endif
other.c
#include "other.h"
#include "Global.h"
void do_other_stuff(void)
{
GlobalCounter++;
GlobalStruct.var1 = 100;
GlobalStruct.var2 = 0xFFFFFF;
}