For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2005 > Using make









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using make
JS

2005-03-28, 3:59 pm

I have a c file called test.c

I have then made a Makefile that looks like this:

blop : test.c
gcc -g -Wall test.c

When I type "make test" it compiles the test.c file and generates a test
executable file. But I thought make would only look for the target (blop),
and if no such target is found it would give an error.

Why does it compile test.c when I don't have a test target in my Makefile?

JS
Chuck Dillon

2005-03-28, 8:58 pm

JS wrote:
> I have a c file called test.c
>
> I have then made a Makefile that looks like this:
>
> blop : test.c
> gcc -g -Wall test.c
>
> When I type "make test" it compiles the test.c file and generates a test
> executable file. But I thought make would only look for the target (blop),
> and if no such target is found it would give an error.
>
> Why does it compile test.c when I don't have a test target in my Makefile?
>
> JS


A target is one of the words on the left side of the colon. In your
case the target is blop. Your makefile is telling make that the
command 'gcc -g -Wall test.c' will make a file called 'blop' and also
that the file 'blop' depends on the contents of the file 'test.c'. (If
you added -o blop to your gcc command it would do what make expects.)

If you do 'make blop', (or just 'make' if the blop clause is the
first/default clause), make will try to make sure that the file 'blop'
exists and is newer than the file 'test.c'. If there is no 'blop' file
or if 'test.c' if newer than the 'blop' file, make will execute your
gcc command in an attempt to make 'blop' the file. If 'test.c' doesn't
exists make would complain that it doesn't know how to make 'test.c'.

Make does not interpret your gcc command and determine what it
generates. It assumes the gcc command will create 'blop' since you
told it that it would.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
JS

2005-03-28, 8:58 pm

Chuck Dillon wrote:

> JS wrote:
>
> A target is one of the words on the left side of the colon. In your
> case the target is blop. Your makefile is telling make that the
> command 'gcc -g -Wall test.c' will make a file called 'blop' and also
> that the file 'blop' depends on the contents of the file 'test.c'. (If
> you added -o blop to your gcc command it would do what make expects.)
>
> If you do 'make blop', (or just 'make' if the blop clause is the
> first/default clause), make will try to make sure that the file 'blop'
> exists and is newer than the file 'test.c'. If there is no 'blop' file
> or if 'test.c' if newer than the 'blop' file, make will execute your
> gcc command in an attempt to make 'blop' the file. If 'test.c' doesn't
> exists make would complain that it doesn't know how to make 'test.c'.
>
> Make does not interpret your gcc command and determine what it
> generates. It assumes the gcc command will create 'blop' since you
> told it that it would.
>
> -- ced
>
>


Ok then in this situation where I only have one file that needs to be
created there is no reason for a target name because the System command
will be executed even if I just type "make".
Fletcher Glenn

2005-03-28, 8:58 pm

JS wrote:
> Chuck Dillon wrote:
>
>
>
>
> Ok then in this situation where I only have one file that needs to be
> created there is no reason for a target name because the System command
> will be executed even if I just type "make".


Since you never actually create the file called "blop", then
make will build an a.out file. This a.out file will be created
every time you use "make" (whether or not the file test.c has
changed) because, make will never find the file "blop" to compare
to the datestamp on test.c.

--

Fletcher Glenn

shakahshakah@gmail.com

2005-03-29, 3:59 pm

Chuck Dillon wrote:
> JS wrote:
test[color=darkred]
(blop),[color=darkred]
Makefile?[color=darkred]
>
> A target is one of the words on the left side of the colon. In your
> case the target is blop. Your makefile is telling make that the
> command 'gcc -g -Wall test.c' will make a file called 'blop' and also


> that the file 'blop' depends on the contents of the file 'test.c'.

(If
> you added -o blop to your gcc command it would do what make expects.)
>
> If you do 'make blop', (or just 'make' if the blop clause is the
> first/default clause), make will try to make sure that the file

'blop'
> exists and is newer than the file 'test.c'. If there is no 'blop'

file
> or if 'test.c' if newer than the 'blop' file, make will execute your
> gcc command in an attempt to make 'blop' the file. If 'test.c'

doesn't
> exists make would complain that it doesn't know how to make 'test.c'.
>
> Make does not interpret your gcc command and determine what it
> generates. It assumes the gcc command will create 'blop' since you
> told it that it would.


Why doesn't make complain about "No rule to make target 'test'",
though?
Does make pick up a default ".c => (no extension)" rule?

I tried the OP's makefile on Solaris, got the following:

jc@soyuz:/tmp> cat Makefile
blop: test.c
gcc -g -Wall test.c

jc@soyuz:/tmp> make
gcc -g -Wall test.c
test.c: In function `main':
test.c:11: warning: implicit declaration of function `open'

jc@soyuz:/tmp> rm a.out
jc@soyuz:/tmp> make test
cc test.c -o test

jc@soyuz:/tmp> rm test
jc@soyuz:/tmp> make bar
make: *** No rule to make target `bar'. Stop.

Chuck Dillon

2005-03-29, 3:59 pm

shakahshakah@gmail.com wrote:

>
>
> Why doesn't make complain about "No rule to make target 'test'",
> though?
> Does make pick up a default ".c => (no extension)" rule?


Somehow I failed to address the OP's specific question. Must have been
an off day.

Yes it tries to match the request via implicit rules. See the make
manpage on Solaris for the strategy it uses. It knows that .c files
beget .o files which beget executables. test.c is in the dependency
tree so when you ask to make test it guesses you want to make an
executable out of test.c. You can also do 'make test.o' and it will do
the right thing.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com