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

Asked

Viewed 960 times

3

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 the vetores.c, as well as calling the functions of ordena.c and uniao.c in the vetores.c, and the functions of ordena.c in uniao.c and others... I tried using files headers (.h) thus:

global. h

#ifndef _GLOBAIS_H_
#define _GLOBAIS_H_

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

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

ordains. h

#ifndef _ORDENA_H_
#define _ORDENA_H_

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

#endif

union. h

#ifndef _UNIAO_H_
#define _UNIAO_H_

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

#endif

And in the archives .c 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"

1 answer

3


The little code shown clearly indicates that there should be no such separation. Apparently, the whole code has a direct connection, and it’s important that he be there. A separation should only occur if there is a reason for it, it needs to have a separate functionality, which does not occur in this case.

The file must function as a logical unit. It works, very roughly speaking, as it is a class in object-oriented languages. There can only be that which has direct relation to that and everything that has direct relation must be there alone and not in other places.

The problem is even bigger when using global variables. This should be avoided, but since you used it increases the reason for everything being together.

Archives header only exist to declare elements that will be used in other contexts. If everything is in one file, there is no reason why.

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

It may be interesting to separate is the main(), but it depends on understanding a broader context, in this case I doubt. And if it is to separate there the header of the other functions would be useful. Reinforcement that does not seem to be the case.

Another point that should not be made is to include a #include inside each other to be inserted into the code .c. The #include should be used in the file where it is needed, should not delegate its inclusion to other files, this ends up causing chaos in larger projects.

Even if the intention is to learn how to break up, in which case, it’s best not to do this. And if you’re going to simulate it, you’d need access to all the code to see what should be in each file. Without seeing everything and knowing what the problems are, you can’t answer more than this.

The ideal is to go the simplest way and do what is mastered.

  • Okay, thanks for the explanation, it was really for educational questions, just to understand how it works etc, I’m more used to other languages. The global ones were created just to make this separation, because in the future I would need to indicate them in several places (the program will grow yet), I will return them as places.

  • 1

    The more the program grows, the less it should use global variables. In fact, the ideal is that they do not exist in programs. I even understand the motivation, but it is no use doing something wrong to learn the right concept. If you have a better context, where separation is natural and necessary, it’s easier to show how. Other than that, it’s all right in the code, it should work (of course I didn’t see the rest).

  • In fact the global variables wouldn’t even need to exist in this case, just use pointer.. Thank you there.

  • Yes, we can avoid global variables in 100% of cases.

  • In the case of the archives, I analyzed and could see the problem, I was including . h in wrong places, I did the following: I declared the only . h with the same name in . c and all . h in main, another error was in marriage. h not having included orneda.h.. I understood how it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.