JZ555
JZ555

Reputation: 637

How to clean object files without using command 'make clean' when compiling with makefile?

What I want to know is how to write a makefile that compiles a program and then removes the object files created in the process with just using command 'make' so I don't have to run command 'make clean' afterwards. Here's my current makefile:

prog: prog.o prog_func.o
    gcc prog.o prog_func.o -o prog -Wall -std=c99   
prog.o: prog.c prog_func.h
    gcc prog.c -c -Wall -std=c99 
prog_func.o: prog_func.c prog_func.h
    gcc prog_func.c -c -Wall -std=c99
clean:
    rm prog.o prog_func.o

EDIT:

And this is how it's done:

prog: prog.o prog_func.o
    gcc prog.o prog_func.o -o prog -Wall -std=c99
    rm prog.o prog_func.o
prog.o: prog.c prog_func.h
    gcc prog.c -c -Wall -std=c99 
prog_func.o: prog_func.c prog_func.h
    gcc prog_func.c -c -Wall -std=c99

It works now. Thanks for quick responses.

Upvotes: 1

Views: 15632

Answers (3)

HottyDunker
HottyDunker

Reputation: 91

I have just taken a sample example.

makebuild: clean prog                                
prog: 
    gcc a.c -o a.o
clean:
    rm a.o

Running 'make' from command line will first 'rm' a.o file and then run 'gcc a.c -o a.o'. The only problem here is that it will not work for first time as makebuild is calling clean first and it will throw an error as it will not find a.o file. You have to put check that 'rm' only when a.o file is present.

something like this:

clean:
    if a.o exist
        rm a.o

Upvotes: -1

Saravanan
Saravanan

Reputation: 163

After building executable, you just give the same command (rm command) after the executable command

Upvotes: 3

Isuru Madusanka
Isuru Madusanka

Reputation: 373

How about compile without making f files like this

gcc -Wall -o prog prog.c

this command will not produce o files.

Upvotes: 0

Related Questions