Including source files in C

So, I get the point of headers and source files. What I am not getting is how the compiler knows to compile all the source files. Example:

example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

int example(int argument); // prototype

#endif

example.c

#include "example.h"

int example(int argument)
    {
    return argument + 1; // implementation
    }

main.c

#include "example.h"

main()
    {
    int whatever;
    whatever = example(whatever); // usage in program
    }

How does the compiler, when compiling main.c, know the implementation example()when it does not include anything example.c?

Is this some kind of thing in the IDE where you add files to projects and stuff? Is there a way to do this “manually” as I prefer a simple text editor for dodgy IDEs?

+5
source share
5 answers

, ( make , IDE), , . . , .

+5

C ++ .

(). , - example(), - . , , ()

.

gcc,

gcc -c example.c -o example.o
gcc -c main.c -o main.o
gcc example.o main.o -o myProgram

2 gcc - . - .

+9

... .
, gcc, :

gcc file1.c -c -ofile1.o
gcc file2.c -c -ofile2.o

, , , (, ), - . :

gcc file1.o file2.o -oexecutable

"" , .. , . ...

IDE-, Google make

+4

example() main.c - ( ), . .o , . IDE Makefile. Makefiles , , . , Makefile.

+1

. . IDE make. Makefile s.

0

All Articles