Creating your own header file

Asked

Viewed 29,291 times

14

Someone could explain to me the utility and how to create the header file in c with an example?

3 answers

15


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

  1. Compilation: Translate each source (.c) (called translation unit) on an object (.o)
  2. Linking: Merge all objects into an executable program or library

The point of this separation is to buy time. First that the compilation occurs in parts, one file at a time, avoiding saturating the memory and optimization time. After that you just need to recompile the sources you change, not all.

Thus the compiler has no information about what functions and variables are exposed by the other translation units, only what it is dealing with at the moment. Header files are the way to specify this interface. The compiler considers that anything you declare but do not define will be set in another unit. So group these statements into files. By #including one file in another you are just copying everything you have in it. It is also common to include the header in the file that implements it (defines its statements) as a way to cause errors if you define something different 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’re 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

6

Header files, header files, are libraries, in them there is collection of functions. It is exactly the same as stdio.h, string.h and other commonly used libraries, including in the archive .c is made with the symbols <>, #include <stdio.h>, because the file stdio.h is located in a folder called include, which in turn is inside the compiler folder.

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

Benefits:

  1. Reuse of code. It becomes easier later, in a project, to reuse pre-made and compiled functions, it is not necessary to reimplementthem.
  2. Organizing. Omitting the implementation, only the logic used in the main file is seen by the programmer, there are fewer lines to analyze. And it is assumed, that the functions are correct, is a good starting point to find future errors in the use of them.
  3. Omission of implementation functions, classes and/or methods. With this, the user is limited to the library interface, has no idea of the implementation, and you end up having secrecy about the technique used, which can be only yours. This also avoids improper changes and, in a way, inappropriate uses, since you specify how to use a certain function.

Organizing:

In the archive helloWorld. h you put prototypes, variables, anyway, statements.

 #ifndef HELLOWORLD_H
 #define HELLOWORLD_H

 void helloWorld();

 #endif

In the archive helloWorld. c you will make the implementations.

 #include "helloWorld.h"
 #include <stdio.h>

 void helloWorld()
 {
    printf("Hello World!");
 }

And in the main. c, the main file, will make proper use of what has been implemented in the file helloWorld. c.

 #include "helloWorld.c"

 int main(void)
 {
    helloWorld();
    return 0;
 }

To compilation sequence must, of course, be first the implementation (helloWorld. c), for later you can use it - I am considering here the use of an IDE as Code::Blocks, that compiles and links directly, directly generating the executable and running it, including.

There is also the possibility to compile at the command prompt, the compilation order does not matter this way, as the headers will be included in the link of the object codes (.the) and not in the compilation, process that will be done about 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

4

Header files are only used to define what you want 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 yourself in headers, you are looking for the definition of the functions/variables, not the implementation of it, which in this case is stored in the files. obj if I’m not mistaken at compilation time...

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)

you not only need the dll (which is compiled code - just like .obj), you need the headers (headers) that define the functions to be used...

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

I hope I was clear in the explanation!

  • Thank you very much, clarified a lot

  • to use it I have to give an import, right? Just that?

  • @user2984406 You will give one #include "seuheader.h"

Browser other questions tagged

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