How to import functions from another file in C?

0

In Java if we create other classes, such as classe1 and classe2 , if I want to call classe1 within classe2 (since they are in seprados files) I use import classe1 / p>

I made this code in C, it's an old game I have to use some computational intelligence features. I always like to separate my codes with business rules and ui (user interface) ie "words, questions" go in one "file" and the "code" goes in another "file".

How do I call another file inside another C file, same as I quoted in Java? In order to use functions from other files ...

    
asked by anonymous 21.09.2016 / 16:43

3 answers

3

Unlike Java in C, the statement is often separated from the definition of functions. It is common for the definition to go into a .c file and the statement is in a file called the .h header. But this is not required.

Contrary to what many people believe the prototype (function declaration) is required only when it will use in external file - the intention here - or when there are cyclical references between functions. The way the question is used is totally useless if you change the order of the functions.

When using functions from another file, the common thing is to use the #include of this .h to include the definitions and the compiler knows how to handle that.

All codes, usually .c , need to be compiled. This is done manually. They also need to be linked together somehow, either during compilation or later, when they already have something precompiled.

The question does not give enough information to give a more detailed answer, but it would be basically what it already did with language components:

#include <seuOutroArquivo.h>

Of course, you do not always have to do this. It depends on the context, on the concrete case you are making. To decide the right way you have to learn the whole, understand the philosophy of language.

In this file it would have something like this (I'll kick it since the question does not help):

int algumaRegraDeNegocio(int);

And there in a file .c would have something like this:

int algumaRegraDeNegocio(int valor) {
    //faz alguma coisa aqui
    return 1;
}

Obviously this is a huge simplification.

To better understand header operation.

See: What's the difference between declaration and definition?

Note that there is a very large gap between Java and C. If you try to play in C what you did in Java it will go very wrong. I say this because it seems that you are trying to use the philosophy of one in the other.

    
21.09.2016 / 18:06
0

Simple example of organizing a project's files in C language. (The function names are illustrative only, not a scheme to follow!)

File .... / game / main.c

#include "rg/rg.h" // regras de negocios
#include "ui/ui.h" // interface com usuario

// constantes visiveis apenas neste arquivo fonte
enum { N_THINGS = 100, N_OTHER_THINGS = 42 };

// funcoes locais a este arquivo fonte
static void funcLocal_01(void);
static void funcLocal_02(void);

// variaveis globais visiveis apenas neste arquivo fonte
static int nThings;
static char nameThings[N_THINGS];

int main(void)
{
   nThings = 10;
   abcd = 0; // esta' no "modulo" rg

   if (xyz == 123) // esta' no "modulo" ui
   {
      // bla bla bla
      // bla bla bla
   }

   func_ui_001();
   func_rg_xyz();

   funcLocal_01();

   func_rg_xxx();
   func_ui_zzz();

   funcLocal_02();
}

void funcLocal_01(void)
{
   // bla bla bla
   // bla bla bla
}

void funcLocal_02(void)
{
   // bla bla bla
   // bla bla bla
}

File .... / game / rg / rg.h

extern int abcd;

extern void func_rg_xyz(void);
extern void func_rg_xxx(void);

File .... / game / rg / rg.c

// variavel exportada
int abcd = 1;

// variaveis locais a este arquivo fonte
static int xyz;
static char array[54];

// funcoes locais este arquivo fonte
static void func1(void);
static void func2(void);

void func_rg_xyz(void)
{
   // bla bla bla
   func1();
   // bla bla bla
}

void func_rg_xxx(void)
{
   // bla bla bla
   func2();
   // bla bla bla
}

void func1(void)
{
   // bla bla bla
   // bla bla bla
}

void func2(void)
{
   // bla bla bla
   // bla bla bla
}

File .... / game / ui / ui.h

extern int xyz;

extern void func_ui_001(void);
extern void func_ui_zzz(void);

File .... / game / ui / ui.c

int xyz = 49;

void func_ui_001(void)
{
   // bla bla bla
   // bla bla bla
}

void func_ui_zzz(void)
{
   // bla bla bla
   // bla bla bla
}
    
23.09.2016 / 03:46
0

Here is an example example.c file that you will compile (/home/andre/workspace/c/example.c)

#include </home/andre/workspace/c/andruida.c>
int main(){
printa(100);
printo();
return 0;
}

Here below a file described in the file include above (/home/andre/workspace/c/andruida.c)

#include <stdio.h>

int printa(int l){
    while (l--)printf("PRINTA\t");
    printf("\n");
    return 0;
}

int printo(){
    printf("\n\n\nPRINTO\n");
    return 0;
}

How do I use gcc in the shell:

gcc -o exemplo exemplo.c

See that I did not even need to mention andruida.h

    
22.09.2016 / 02:12