LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-31-2013, 05:28 PM   #1
TheBigH
LQ Newbie
 
Registered: Dec 2009
Posts: 12

Rep: Reputation: 0
Trouble with Makefile dependencies and recipes


Sorry if this is the wrong subforum; mods feel free to move it.

I am having trouble with make not compiling prerequisites according to the rule I've specified. Or perhaps I'm just misunderstanding something.

Suppose I have a Makefile like this:

Code:
derp: derp.c derp.h herp.o
        gcc `abunchofflags` -o derp derp.c foo.o `abunchoflibs`

foo: foo.c
        gcc `morestuff` -c foo.c
What I would expect to happen if I run "make derp" is that it first compiles foo.o according to the recipe I put in the Makefile, and then compiles derp. But what it actually does is compile foo.o with
Code:
cc     -c -o foo.o foo.c
i.e. NOT with the recipe in the Makefile, and then compiles derp properly. If I run "make foo", it does compile foo.o the way I want. Again, if foo itself has prerequisites it compiles those wrong but foo itself correctly.

Why does this happen? How do I get make to use the compiling recipe I want for prerequisites?
 
Old 11-02-2013, 05:07 AM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
There are two problems I can see with your recipes.

1. foo.o isn't listed as a dependency of derp

2. The target of the second rule is "foo" and not "foo.o". Hence, when make looks for a way of making "foo.o" it doesn't find a suitable rule so uses its own built-in rules for compiling object files from c.
 
1 members found this post helpful.
Old 11-03-2013, 07:00 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
And it is incomplete.

derp.o depends on derp.c and presumably the include file.

so the dependency for derp is derp.o, not derp.c - it doesn't matter where the .o comes from, only that it exist.

And normally, the `abunchofflags` would be specified by a "CFLAGS=...." which may be
`abunchofflags`

A `bunchoflibs` is USUALLY specified using a "LIBS=..." and then use $(LIBS) on the build command.

As a bit of configuration options, the object files needed for derp can be collected
into a definition: DERPOBS=derp.o foo.o

This would change your Makefile to:
Code:
DERPOBS=derp.o foo.o

LIBS=`abunchoflibs`
CFLAGS=`abunchofflags`

derp: $(DERPOBS)
        gcc -oderp $(DERPOBS)

foo.o: foo.c

derp.o: derp.c derp.h
This makes a simple update as the program expands - only additional object needs for the derp program as they get added to only one line. There are even ways to generate the list of header file dependencies automatically (http://www.gnu.org/software/make/man...epend_007d-284) as the gnu C compiler has an option (-M) to list the header dependencies...

And allow make to do what it does best.
 
1 members found this post helpful.
  


Reply

Tags
makefile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile with multiple dependencies in subfolder kaktusmimi Programming 18 11-01-2012 08:17 AM
Problem with makefile dependencies RichardS Programming 3 07-16-2007 04:54 AM
Newbie needs help: Makefile Dependencies SirChives Linux - Software 6 05-15-2005 08:03 PM
Makefile trouble ar1 Programming 5 05-07-2005 06:36 AM
Trouble with Makefile chui_yap Linux - Newbie 2 01-02-2005 10:03 PM


All times are GMT -5. The time now is 11:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration