C - What are the c source files without include clause

I took the plunge and am learning C. It was a pretty good but controlled learning curve coming from a scripting background (php, perl) just a little bit of C #.

I used the website "Learn C The Hard Way" and am still very good at (think), but I cannot understand this part of one of the exercises:

http://c.learncodethehardway.org/book/ex19.html

He created four source files - object.h, object.c, ex19.h, ex19.c

But I don't understand how the object.c file is included.

The main function is in ex19.c and has the line

#include "ex19.h"

      

The ex19.h file has the line

#include "object.h"

      

But object.h does not refer to including object.c. Interestingly, object.c contains the string

#include "object.h"

      

Is there some kind of implied include where if you include the header file it will automatically include the c source with the same name?

+3


source to share


3 answers


This is the job of a separate program called the linker. In C source files, you need to access the header files of other C source files so that they can see information about what functions, types, and variables are defined by that second C file that the first file can use. Each C file is then compiled independently of the others. The compiler output is an object file.

To create the final program, a second program, called the linker, comes in and bundles all the object files together into a common executable file. This program has the task of taking the implementations of all the different C files and cross-referencing them against each other, so that every time one C file refers to a function or variable in another C file, that reference can actually be made to the corresponding object.



This is why you don't need to include files .c

. Once a source file has a header, it knows enough about the other file to use the functions it provides to the compiler to make sure it uses them correctly. The linker then handles the cross-reference work. You can think of the compiler as a program that checks that if functions are defined, the program will work. The linker then checks to see if these functions are defined in the first place and sets the appropriate links in the executable.

Hope this helps!

+6


source


The .c object is not included.

It is compiled as a native block and includes object.h

See the make file:

CFLAGS=-Wall -g

all: ex19

ex19: object.o

clean:
  rm -f ex19

      

ex19: object.o

tells you that object.o must be created before ex19 can be created, and by default it is fetched from the make file as object.c.



So this make file says

to build everything you need ex19 to get ex19 you need object object.o and to create object.o the make file grabs object.o created from object.c

From the page you link to:

make doesn't see anything in the file for object.o, but it does see object.c, and it knows how to turn .c into .o, so that.

+1


source


All the logic lies in the makefile and the smart compiler. The final binary has an object file named object.o, which ideally contains all the function definitions defined in the object.h file. It is a linker that links functions declared in the .h file with the definition that is available in the .o file.

0


source







All Articles