Makefiles?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Makefiles?

Post by pcmattman »

I am sick to death of using batch files to build my OS. They are slow, there is no way of not compiling already compiled objects that haven't changed (etc...). The problem is, the makefile tutorials I've read have not helped at all. I still don't understand them.

What I need is a makefile that will compile all the '.c' files in the directory, then link them into the final ELF file (using GCC, ld etc...). Any help would be greatly appreciated.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Makefiles?

Post by Candy »

pcmattman wrote:I am sick to death of using batch files to build my OS. They are slow, there is no way of not compiling already compiled objects that haven't changed (etc...). The problem is, the makefile tutorials I've read have not helped at all. I still don't understand them.

What I need is a makefile that will compile all the '.c' files in the directory, then link them into the final ELF file (using GCC, ld etc...). Any help would be greatly appreciated.

replace all space-indentations with tabs

Code: Select all

TARGET=output.elf
OBJECTS=a.o b.o c.o
LD=ld
CFLAGS=-Wall -Wextra

all: $(TARGET)

$(TARGET): $(OBJECTS)
    $(LD) -o $@ $^
The CFLAGS should work when compiling objects, you can leave them out if you just want a working makefile. The rest is pretty generic. If you have a more specific question about a bit of any makefile, just ask.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

How can I tell it to do this on all the objects (like a *.o or something like that)? Or do I have to type it in myself? Because I have about 20 different files and to put all of them into the makefile won't be fun.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

pcmattman wrote:How can I tell it to do this on all the objects (like a *.o or something like that)? Or do I have to type it in myself? Because I have about 20 different files and to put all of them into the makefile won't be fun.
Well... you need to either type in the list of objects once or you need to change this to automatically take the directory worth of contents for object outputs. I prefer the first, but you may prefer the second.

Short & simple: put all the objects behind OBJECTS= and it'll just work.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

In my makefile I do this to find all of the source files:

Code: Select all

# all of the directories that contain code
PROJDIRS := includes libs asm

# get the source files
SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
then you could do something like this to get a list of the object files

Code: Select all

# get a list of the object files
OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

[wiki]Tutorial:Makefile[/wiki] will hopefully help you understanding Makefiles a bit better.
Every good solution is obvious once you've found it.
Andrew275
Member
Member
Posts: 30
Joined: Tue Feb 27, 2007 2:29 pm
Contact:

Post by Andrew275 »

Also, if you can get your hands on the O'Reilly book about GNU Make, it will explain a lot.
My operating system: Desktop OS
Content management system/forum: Deluxe Portal
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Just one thing, I'm on a Windows box... Also, I've tried to go through the tutorial and also tried the suggestions presented here. None of them work. I need help!
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

You could try a different make system. I never had much luck with any GNU tools on Windows. CMake and Scons come to mind.
C8H10N4O2 | #446691 | Trust the nodes.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Thanks Alboin. I'll look into those.

One question: is there any non-makefile way to only compile files that have been modified?
Zekrazey1
Member
Member
Posts: 37
Joined: Sat Mar 10, 2007 8:28 am

Post by Zekrazey1 »

What make are you currently using? Different vendor's makes have different syntax, features, etc.
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:

Post by Combuster »

Cygwin does a good job at running unix stuff. I run make that way and it works wonders :)

As for non-makefile methods:
Perl, anyone? :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 ]
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

pcmattman wrote:Just one thing, I'm on a Windows box... Also, I've tried to go through the tutorial and also tried the suggestions presented here. None of them work. I need help!
Have you tried Cygwin? I use cygwin on windows and most of the stuff that I see in makefile tutorials works.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

The tutorial in the wiki assumes GNU make. The Makefile in question was created on Cygwin.
Every good solution is obvious once you've found it.
Post Reply