How to separate global functions / variables from the program into files?

2

I have some files:

vetores.c // função main
uniao.c // função para unir vetores
ordena.c // função para ordenar vetores
globais.c // arquivo com variáveis globais

I want to know how I reference one file in the other.

Example: I need to use the variables of globais.c in vetores.c , as well as calling the functions of ordena.c and uniao.c in vetores.c , and% in ordena.c and others ... I tried using headers ( uniao.c ) files like this:

globals.h

#ifndef _GLOBAIS_H_
#define _GLOBAIS_H_

#include<stdio.h>
#include<stdlib.h>

int *A, *B, ta, tb;
#endif

Order.h

#ifndef _ORDENA_H_
#define _ORDENA_H_

int* ordena(int *vet, int tam);

#endif

uniao.h

#ifndef _UNIAO_H_
#define _UNIAO_H_

int* uniao( int *vet1, int tam1, int *vet2, int tam2);

#endif

And in the files .h I used references like this:

#include"uniao.h"
#include"globais.h"
#include"ordena.h"

I also tried straight through .c :

#include"uniao.c"
#include"globais.c"
#include"ordena.c"
    
asked by anonymous 04.12.2015 / 19:57

1 answer

2

The bit of code shown clearly indicates that there should not be such a separation. Apparently all the code has a direct relationship and it is important that it is together. A separation should only occur if there is a reason for this. It needs to have a separate functionality, which does not occur in this case.

The file should act as a logical drive. It works, quite roughly, as a class in object-oriented languages. There can only be that which is directly related to it. And everything that has direct relation must be only there and not in other places.

The problem is even greater when using global variables. You should avoid this, but since you have used it, it raises the reason for everything.

header files should only exist to declare elements that will be used in other contexts. If everything is in one file, it does not have to exist.

They were invented to solve an organization problem and not to use without having a reason. It should only be used if it finds a good explanation for its use.

What might be interesting to separate is main() , but depends on understanding a broader context. In this case, I doubt it. And if it is to separate, then the header of the other functions would be useful. Reinforcement does not seem to be the case.

Another point that you should not do is to include a #include within the other so that it is inserted into the .c code. The #include must be used in the file where it is needed, its inclusion should not be delegated to other files. This ends up causing chaos in larger projects.

Even if the intention is to learn how to do the separation, in this case, it is best not to do this. And if it's to simulate, you'd have to have access to the whole code to see what should be in each file. Without seeing everything and knowing what the problems are, you can not even respond more than this.

The ideal is to go the simpler way and do what you master.

    
04.12.2015 / 20:22