Make a Makefile

P

Paminu

I have the following three files:

link.h
contains my structs and list of functions.

link.c
contains my implementation of the functions

linktest.c
contains my main function that test the program with some values.


If I only make changes in linktest.c there would be no reason to recompile
link.c

The first time I compile the whole program I do:

gcc -c link.c
gcc -o linktest linktest.c tree.o


But when I only edit the linktest.c file there is no reason to run the first
line: "gcc -c link.c"

Is it possible to make that kind of "rule" in a makefile?


I have tried to write the follwoing make file:

treetest: tree.c
gcc link.c -c
gcc -o linktest linktest.c link.o


But this way both the gcc commands gets run each time.
 
M

Michael Rasmussen

treetest: tree.c
gcc link.c -c
gcc -o linktest linktest.c link.o
Try this instead:

OBJ = link.o linktest.o

all: treetest

treetest: $(OBJ)
gcc $(OBJ) -o treetest

link.o: link.c link.h
gcc -c link.c link.h

linktest.o: linktest.c
gcc -c linktest.c
 
M

Michael Rasmussen

But this way both the gcc commands gets run each time.
Try this instead:

OBJ = link.o linktest.o

all: treetest

treetest: $(OBJ)
gcc $(OBJ) -o treetest

link.o: link.c link.h
gcc -c link.c link.h

linktest.o: linktest.c
gcc -c linktest.c
 
J

Joe Wright

Michael said:
Try this instead:

OBJ = link.o linktest.o

all: treetest

treetest: $(OBJ)
gcc $(OBJ) -o treetest

link.o: link.c link.h
gcc -c link.c link.h

linktest.o: linktest.c
gcc -c linktest.c

Really? 'gcc -c link.c link.h' compiling a header? Surely link.c will
have '#include "link.h"' and if not, why not?
 
K

Keith Thompson

Paminu said:
I have the following three files:

link.h
contains my structs and list of functions.

link.c
contains my implementation of the functions

linktest.c
contains my main function that test the program with some values. [snip]
Is it possible to make that kind of "rule" in a makefile?

This is not a C question. The "make" command comes with
documentation; you should find the answer there. Failing that, try
comp.unix.programmer.

(Quick hint: the answer is yes.)
 
K

Keith Thompson

Joe Wright said:
Really? 'gcc -c link.c link.h' compiling a header? Surely link.c will
have '#include "link.h"' and if not, why not?

And this kind of thing is *exactly* why we discourage answers to
off-topic questions here. (Actually it's only one of a number of
reasons.)
 
F

Flash Gordon

Michael said:
Try this instead:

OBJ = link.o linktest.o

<snip>

Can you please take your discussion of Makefiles somewhere appropriate.
Possibly comp.programmer, although I am not certain about that, check
their FAQ and a sample of posts first. Makefiles are not, and never have
been, part of C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top