I'm trying to clean up my project, to help expand it further, but there's about 200 lines of variables definitions, calculations and writing into arrays before starting the actual application, and it's an absolute mess.
Is there a way to put all of this into another file, and include these definitions in the file I'd like to use it in (and only that file, to avoid conflicts) ?
I tried creating something like "levelVars.c" and including it in the "level.c" file, but all I get is a bunch of errors.
There's also some custom types and SDL types in here, so..it might cause problems.
The reason I want to do all this is to clean up the file : I'm having trouble navigating between everything with such a massive block of variables. I also can't reduce their numbers, as I need them all ; every variable is taken in by some functions and used by others, so I can't just reduce their scope and clean up this way. Well, I could maybe cut down ten variables like this, but it won't help much.
The beginning looks like this :
int trackSomething = 0;
int trackSomethingElse = 0;
int yetAnotherCount = 0;
bool active = false;
bool andAnother = false;
bool iThinkYouGotIt = false;
int arr[SIZE_1][SIZE_2];
for(int i = 0 ; i < SIZE1 ; i++)
{
for(int j = 0 ; j < SIZE2 ; j++)
{
arr[i][j] = 0;
}
}
....
while(active)
{
// The actual loop that does something meaningful with all this
}
Don't use the pre-processor to do includes. Use the linker:
$ cat variables.h
extern int d;
$ cat variables.c
int d = 57;
$ cat main.c
#include "variables.h"
#include <stdio.h>
int main(void) { printf("d = %d\n", d); return 0; }
$ gcc -c main.c
$ gcc -c variables.c
$ gcc main.o variables.o
$ ./a.out
d = 57
Your request that the variables only be available to one translation unit is somewhat difficult to enforce, and you really shouldn't try. (Ab)using the pre-processor to include the variable definitions with a #include to force the definitions and their usage to all be in the same translation unit will do it, but your code will be better organized if you don't do that.