4

In problem set 4 of cs50, there is a main function filter.c, which uses header file helper.h. The latter contains the prototypes of a few functions, and these complete functions are written out in helper.c

My question is: since filter.c only connects to helper.h, how does it connect to the code of the functions? Or does filter.c automatically also look for a .c file with the same name as the linked header file?

I hope this is somewhat clear!

BalGu
  • 53
  • 1
  • 7
Zegher V
  • 117
  • 7

1 Answers1

3

The compilation of a C program involves three basic steps listed below shortly:

Preprocessing:

the preprocessor takes a C source code file and deals with the #includes, #defines and other preprocessor directives. also clean all the comments and expand all the macros in those files. (it performs both on c and h files) The output of this step is a "pure" C file without pre-processor directives.

Compilation:

the compiler takes the pre-processor's output and produces an object file from it. written in assembly that suits the cpu your code will run on it.

Linking:

the linker takes the object files produced by the compiler and produces either a library or an executable file. which have all the information and a process terms to be run directly by the os (.out or .elf are examples of the linker output).

your case

In filter.c or in each .c file (including the helper.c), where you #includes "helper.h", this step ended with all functions signature declared in helper.h copy paced in filter.c (only signatures). now this happen in preprocessor time. now in main.c where (i suppose) you #includes "filter.h", the preprocessor will copy all the nested .h files (tell reaching last .h file in the chain) into main.c (in our case its only filter.h that contains the helper.h). when compiler comes to compile the .c files, he compiles helper.c, 'filter.c and main.c and generates object files (.o or .obj - binary files suited for current cpu) at end he triggers the linker to link all these objects together to become and fully executable (linker also links all functions that came from predefined libs like printf for e.g.)

                 preprocessor                    compiler                 linker
                 ------------                    --------                 --------
+------------+ copy helper.h to    +----------+         
+  helper.h  +------------------>  + filter.c + -------> filter.o +      +--------+
+------------+        |            +----------+                   |      +        +
                      |                                           |      + .elf   +
                      |                                           +--->  +        +
                      +--------->  +-----------+                         +  or    +
                                   + helper.c  + ------> helper.o ---->  + .out   +
                                   +-----------+                  +--->  +--------+
                                                                  |          ^
        +-------------+                                           |          |
        + filter.h    +---------> +-------------+                 |          |
        +-------------+           + main.c      + -----> main.o---+          | 
                                  +-------------+                            |  
                                                                             |
                                                         +-------------------+
                                                         + libs (e.g. stdio) +
                                                         +-------------------+

Community
  • 1
  • 1
Adam
  • 2,820
  • 1
  • 13
  • 33