Ewan Delanoy
Ewan Delanoy

Reputation: 1282

How can main.o not be created last during compilation of C project?

I've watched the installation of GNU make on my computer wth some attention. It is a relatively simple compilation : it creates successively an .o file from each .c file, then creates an executable by linking all the .o files, in the order in which they were created :

gcc  -g -O2 -rdynamic  -o make ar.o arscan.o commands.o default.o dir.o expand.o 
file.o function.o getopt.o getopt1.o guile.o implicit.o job.o load.o loadapi.o 
main.o misc.o output.o read.o remake.o rule.o signame.o strcache.o variable.o 
version.o vpath.o hash.o remote-stub.o glob/libglob.a

What I don't get here is the following : how can main.o not appear last on this list ? Because using the executable on some arguments is equivalent to calling function main in main.c on those arguments, so anything created after main.o seems useless.

Upvotes: 0

Views: 100

Answers (2)

dbush
dbush

Reputation: 223972

The order in which object files are specified in the gcc command line are irrelevant. All are linked into an executable, and that executable knows where the main function lives.

When all object files are read in, the linker ensures that the main function exists along with any functions called directly or indirectly from main.

Just because an object file is called main.o doesn't necessarily mean that the main function lives in that file.

Upvotes: 3

fuz
fuz

Reputation: 93014

The other files contain functions called by main directly or indirectly. Without them, make can't function.

Upvotes: 0

Related Questions