Creating your own header file

12

Would anyone know to explain the utility and how to create the header file in c with an example?

    
asked by anonymous 01.02.2014 / 03:46

3 answers

11

The compilation of a program written in C is summarized in two steps:

  • Compilation: Translate each source (.c) (called translation unit ) into an object (.o)
  • Linking: Merge all objects into an executable program or library
  • The purpose of this separation is to gain time. First, the compilation occurs in parts, one file at a time, avoiding saturation of memory and optimization time. After you just need to recompile the sources you change, not all.

    In this way the compiler has no information on what functions and variables are exposed by other translation units, just the one it is currently dealing with. Header files are the way to specify this interface. The compiler considers that anything you declare but do not set will be set on another drive. So these statements are grouped into files. When #include one file in another you are just copying everything in it. It is common to also include the header in the file that implements it (defines its declarations) as a way to cause errors if you define something other than what you initially stated. Another utility is to use the functions before you define them yourself. An example:

    foobar.h :

    #ifndef FOOBAR_H   // guardas de cabeçalho, impedem inclusões cíclicas
    #define FOOBAR_H
    
    extern int baz;    // declaração de uma variável global
                       // como é global, valor inicial é zero.
    
    int foo(long arg); // declaração de uma função
    int bar(void);     // outra
    
    #endif
    

    foobar.c :

    #include "foobar.h"
    
    int baz; // definição da variável
    
    int foo(long arg) {
        baz += arg;
        return baz + bar() - 2; // só posso chamar bar() aqui porque a declarei antes
    }
    
    int bar(void) {
        return 4;
    }
    

    main.c :

    #include "foobar.c"
    #include <stdio.c> // a libc vai definir essas funções
    
    int main() {
        printf("%d\n", foo(2)); // 4
        printf("%d\n", foo(2)); // 6
        baz = 3;
        printf("%d\n", foo(2)); // 7
    }
    

    Assuming you are using gcc, compile like this:

    gcc -c foobar.c -o foobar.o
    gcc -c main.c -o main.o
    gcc foobar.o main.o -o meuprograma
    
        
    01.02.2014 / 14:27
    5

    Header, header files are libraries, there are collection of functions in them. It is exactly equal to stdio.h , string.h and other commonly used libraries, in this inclusion in the .c file is done with the < > , #include <stdio.h> , because the stdio.h file is located in a folder named include , which in turn is inside the compiler folder.

    When the header file is outside this compiler folder, but in the same breakdown of the code that will use it, double quotes are used, like this: #include "minhaBiblioteca.h" . If the file is in another folder, you will need to step-by-step enter the path in double quotation marks and using / to go to the destination folder of the .h file. > to be used, as in the command prompt.

    Benefits:

  • Code reuse . It is easier then, in a project, to reuse pre-made and compiled functions, you do not need to reimplement them.
  • Organization . Omitting the implementation, only the logic used in the main file is seen by the programmer, there are fewer rows to parse. And it is assumed, that the functions are correct, is a good starting point for finding future errors in their use.
  • Omission of functions, classes, and / or methods. With this, the user is limited to the interface of the library, has no idea of the implementation, and you end up having secrecy about the technique used, which can be yours alone. This also prevents improper changes and, in a way, improper uses, since you specify how to use a certain function.
  • Organization:

    In the helloWorld.h file you put prototypes, variables, and finally declarations.

     #ifndef HELLOWORLD_H
     #define HELLOWORLD_H
    
     void helloWorld();
    
     #endif
    

    In the helloWorld.c file you will do the implementations.

     #include "helloWorld.h"
     #include <stdio.h>
    
     void helloWorld()
     {
        printf("Hello World!");
     }
    

    And in main.c , the main file will make use of what was implemented in the helloWorld.c file .

     #include "helloWorld.c"
    
     int main(void)
     {
        helloWorld();
        return 0;
     }
    

    The compilation sequence should obviously be at first the implementation ( helloWorld.c ), so you can use it later - an IDE such as Code :: Blocks , which compiles and linkedita directly, directly generating the executable and executing it.

    There is still the possibility of compiling at the command prompt, the compilation order does not matter in this way because the headers will be included in the object code ( .o ) and not in the compilation process which will be done over the code in C language ( .c ).

    gcc -c main.c -o main.o
    gcc -c helloWorld.c -o helloWorld.o
    gcc main.o helloWorld.o -o helloWorld
    
        
    01.02.2014 / 13:58
    3

    Header files are used only for the definition of what one wants to use, for example a function

    int soma (int x, int y)
    {
        return x + y;
    }
    

    no header would look like

    int soma (int x, int y);
    

    and no .c / .cpp would be

    int soma (int x, int y)
    {
        return x + y;
    }
    

    Because when you include the headers, you are looking for the definition of the functions / variables, not the implementation of it, which in the case is stored in the .obj files if I am not mistaken at compile time ...

    p>

    This prevents the compiler from recompiling code all the time ...

    A better example is in the case of a dynamic (.dll) or static (.lib) library

    In addition to needing the dll (which is the compiled code - same as .obj), you need the headers that define the functions to be used ...

    So it is not recommended that you write implementations in headers as it speeds up the compilation process if a function has already been compiled

    I hope I have been clear in the explanation!

        
    01.02.2014 / 04:10