MakeFiles

General programming chat and advice for beginners

3 posts
by Johnathon332 » Sat Aug 04, 2012 2:35 pm
Hey, I'm fairly new to linux and looked around the net about makefiles but I can't really seem to make sense of any of them. I was wondering if someone could explain how you would compose your own makefile and what it actually does.

Thank you
Posts: 6
Joined: Thu Aug 02, 2012 10:26 pm
by Gert van Loo » Tue Aug 07, 2012 8:57 pm
I typed "makefiles explained" in google.
I think this one is not too bad:
http://www.google.co.uk/url?sa=t&rct=j&q=makefiles+explained&source=web&cd=3&ved=0CFkQFjAC&url=http%3A%2F%2Fwww.cs.uiuc.edu%2Fclass%2Ffa05%2Fcs225%2Fcs225%2F_resources%2F_tutorials%2FmakeTut.pdf&ei=338hUKWLJtC20QXAzYHQAg&usg=AFQjCNGS2pPf7hTmDVuUtwFSMp6vdaGd6A

However none of the manuals will make much sens unless you have compiled programs yourself and thus
have some idea what linux commands look like, how to compile/assemble/link or in general how to build computer 'stuff'.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2112
Joined: Tue Aug 02, 2011 7:27 am
by gordon@drogon.net » Wed Aug 08, 2012 9:55 am
Johnathon332 wrote:Hey, I'm fairly new to linux and looked around the net about makefiles but I can't really seem to make sense of any of them. I was wondering if someone could explain how you would compose your own makefile and what it actually does.

Thank you


A Makefile is essentially a file with instructions about how to do stuff.

So it contains a simple variable mechanism

foo = 123

and a set of rules that say

this depends on that

which then translate to

if that has changed, then to this

So if you have a project that involves more than one file and you change one file, you only need to re-compile that single file, then link the binaries together. This is much more efficient than recompiling 30 files each time something changes.

Another way to view a Makefile is that it's actually a progrmaming language - you're writing a program to control the comilition of your other programs - however it's a pretty eseoteric "language" if viewd that way.

There are 1000's of resources online about Makefiles and an O'Reilly book (or 2), but if you want some simple exanples, then have a look at my wiringPi library - it uses Makefiles to compile and link the files together.

https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/

Just one thing to remember about Makefiles - spaces and tab's are treated differently! IF you ever copy & paste a Makefile, or bits of one then it may change tabs into spaces and it'll stop working. Thats trips up a lot of newcomes to makefiles!

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1544
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK