On the inclusion of headers (.h) and due compilation

Asked

Viewed 180 times

2

Assuming I create a file .h for the specification of a function

func. h

#ifndef FUNC_H
#define FUNC_H

int xPy(int x, int y);

#endif //FUNC_H

and then created the implementation of this function in a file .c, as:

func. c

int xPy(int x, int y) {
   int result;
   result = x+y;
   return result;
}

I might include the "func.h" in a new file (suppose main.c)

Supposed code of main.c

#include "func.h"

int main() {
   int a = func(10, 10);
   return 0;
}

So far so good, however, in the compilation process does not seem to be enough to do

gcc main.c -o main

I have to do

gcc func.c main.c -o main

I would like to know why this is necessary. The directive #include "func.h" should not make the compiler fetch the file func.c automatically?

In case I’m using GCC 8.2.0.

  • 1

    @Maniero #include <stdio.h> for example, stdio file. h only has the function signature printf, the implementation of this function stays in another file and the additional compiler transparently.

  • 1

    I imagine you wanted to comment on the answer, but let’s go. There you are talking about the library that actually the compiler puts up automatically because he knows everything he needed to know about it and is more to the point in most cases, but note that this is not true in all libraries, the math is not automatic: https://answall.com/q/286771/101

  • 1

    had never used the site to ask questions before, but thanks for the clarifications, helped me a lot

1 answer

3


If you reread your question, analyzing the text carefully, you will already have the answer. It says that you want the func.c be included in your application. You say you’ve used #include "func.h". Where are you including the func.c? In the compiler’s command line. Now, if not put somewhere, and this is the place, as the compiler will know it is to put it. So the direct objective answer to your question is that it needs to be put somewhere, there’s no way the compiler can guess.

Then speculates about it doing automatic. But what automation would this be? How to put the func.h would make the compiler guess that I should put something else? What would that be? I wonder why you put up a file called func extended .h should also put another with extension .c?

That doesn’t make sense, because these names, these extensions are mere conventions. It would actually make pretty little sense because a file .c may need more than one .h or the contrary and a .h refer to implementations that are in more than one .c. What’s more, the .h nor is it mandatory. And I’m not even talking about the extension that could be another, the header is not mandatory (although it might generate some future organizational difficulties, but it’s just something you choose for your project).

One thing has nothing to do with another. Including the inclusion is only a way to bring a text that is in another file to the current file, nothing else, the directive even processes its content, has no intelligence, is only part of a preprocessor, nor is the compiler itself operating there.

You can read more on:

Browser other questions tagged

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