how do you make a *good* makefile?

Programming, for all ages and all languages.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

how do you make a *good* makefile?

Post by earlz »

Ok, well, I have been slowly evolving in my makefile ways, but I have yet to understand how to make those robust makefiles that only compile the files that have changed.

I have yet to find any makefile tutorials out there beyond simple

Code: Select all

default:
    gcc -o name_of_binary -c name_of_source
does anyone have any experience with this kind of makefile?

(also, preferably this will work in both BSD make and GNU make)

edit:

Code: Select all

as-ebc:	${OBJS}
	${CC} ${LDFLAGS} ${OBJS} -o as-ebc

.c.o: ${HDRS}
	${CC} ${CFLAGS} -c $*.c

lint:
	lint ${DFLAGS} ${SRCS}
clean:
	rm -f ${OBJS} as-ebc core a.out errs
install:
	install -s as ${DESTDIR}/bin
that is the best makefile I've seen that works with both bsd make and gnu make. My problem with it though is that I can't figure out a way to put output objs into a seperate ./objs directory and the binary in ./bin/ directory...
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

How about adding paths to your compilation commands? An alternative is to have a move command or something do the job for you after everything is finished, though this wouldn't be the cleanest option IMO.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: how do you make a *good* makefile?

Post by earlz »

I would like it to be a little more elegant... mv also won't clean up stuff if the compile fails, plus I am concerned about those people that build from a read-only source location
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: how do you make a *good* makefile?

Post by bewing »

You create variables for your output destination directories. You do your compilation in two stages.

gcc -c sourcefile.c -o ${objdir}/sourcefile.o

gcc ${objdir}/file1.o ${objdir}/file2.o ... -o ${bindir}/target.bin
ruisleipa
Member
Member
Posts: 46
Joined: Wed Sep 10, 2008 8:39 am
Location: Finland

Re: how do you make a *good* makefile?

Post by ruisleipa »

I don't know if this a is very good solution but hope it is useful:

Code: Select all

CC = gcc
CFLAGS=-Wall -fstrength-reduce -fomit-frame-pointer -funsigned-char -finline-functions \
-nostdinc -fno-builtin -g -I/home/mikko/agileos/include

LD = ld
LDFLAGS = -T link.ld

AS=nasm
ASFLAGS=-f elf -g

OBJS = main.o gdt.o idt.o irq.o isrs.o panic.o string.o \
timer.o kprintf.o ioport.o logo.o multiboot.o cmos.o

all: $(OBJS) 

clean:
	-rm $(OBJS) 
The makefile compiles only the files that have changed after the last compilation or have not been compiled at all.

Work with GNU make. Haven't tried BSD make, may work or not.
http://code.google.com/p/rmmtos/ - Real Mode MultiTasking Operating System
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

berkus wrote:Best makefile is no makefile at all. Use waf/scons/cmake/bam/etcetcetc.
Aren't those (esp. cmake) really makefiles with some different syntax? Probably the easiest would be to use an IDE, but that makes it harder if you want to distribute your source code and make it portable. You could try to build your own tool, though. This kind of tools are amongst the easiest to make.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

berkus wrote:forgot to mention, not counting requiring python installation, the whole waf is redistributable in a 75kb python file, so you can easily commit it together with your os sources and never bother about building or configuring it separately. [/edit]
If its license permits that *and* if it's compatible with your OS's license, then it sounds neat...
Last edited by Love4Boobies on Fri Jan 30, 2009 5:12 pm, edited 1 time in total.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how do you make a *good* makefile?

Post by Combuster »

Not to mention that gnu make is the most often the only tool of its kind to be installed by default. And the same relative amount of people know how to use it.

If you're looking for ideas for OS scale makefiles, have a go at mine. I'm not saying its good but it does everything I want and need. (link in signature)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: how do you make a *good* makefile?

Post by earlz »

the best thign about make(gnu or bsd) ...
it's existed basically forever without really being considered out of date. I mean, that "good" makefile I posted is from BSD 4.2's as (the original BSD for pdp)

I think I just have to become more acquainted with make to develop a makefile just like I like..
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

earlz wrote:the best thign about make(gnu or bsd) ...
it's existed basically forever without really being considered out of date.
Arguably.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: how do you make a *good* makefile?

Post by earlz »

Well, maybe I'll write my own tool to generate a good makefile... (thus, have more control over wildcard resolving)

one problem... in my 3 years or so of working with C... I've never once tried to do anything with directories! D:
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: how do you make a *good* makefile?

Post by Solar »

Have you seen the Makefile tutorial? If it doesn't suit your needs, tell me what's missing and I'll see if I can extend the tutorial...
Every good solution is obvious once you've found it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

And you're making an OS in it? :?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how do you make a *good* makefile?

Post by Combuster »

Solar wrote:Have you seen the Makefile tutorial? If it doesn't suit your needs, tell me what's missing and I'll see if I can extend the tutorial...
It leaves dealing with multiple intermediate/output files (like kernel, separate drivers, bundling into floppy image) as an exercise to the reader. And it doesn't support multiple architectures (speaking of which, a pro's comment on my make settarget hack would be welcome :D).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: how do you make a *good* makefile?

Post by Love4Boobies »

Combuster wrote:(speaking of which, a pro's comment on my make settarget hack would be welcome :D)
I'm no pro, but IMO all you have to do is pass the arch as a command line variable and build according to that (i.e. by passing the right things to your compiler/linker). Also, instead of including all makefiles like you did, I'd do something like this:

Code: Select all

subdirs : foo bar foobar

.PHONY : all $(subdirs)
all :
        $(MAKE) -C $@
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply