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.
@Maniero
#include <stdio.h>
for example, stdio file. h only has the function signatureprintf
, the implementation of this function stays in another file and the additional compiler transparently.– Tom
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– Maniero
had never used the site to ask questions before, but thanks for the clarifications, helped me a lot
– Tom