Page 1 of 1
Makefiles?
Posted: Fri Mar 09, 2007 10:44 pm
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.
Re: Makefiles?
Posted: Sat Mar 10, 2007 2:54 am
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.
Posted: Sat Mar 10, 2007 4:02 am
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.
Posted: Sat Mar 10, 2007 6:44 am
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.
Posted: Sat Mar 10, 2007 8:46 am
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))
Posted: Sat Mar 10, 2007 9:28 am
by Solar
[wiki]Tutorial:Makefile[/wiki] will hopefully help you understanding Makefiles a bit better.
Posted: Sat Mar 10, 2007 11:51 am
by Andrew275
Also, if you can get your hands on the O'Reilly book about GNU Make, it will explain a lot.
Posted: Sat Mar 10, 2007 8:33 pm
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!
Posted: Sat Mar 10, 2007 9:05 pm
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.
Posted: Sat Mar 10, 2007 9:12 pm
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?
Posted: Sun Mar 11, 2007 2:17 am
by Zekrazey1
What make are you currently using? Different vendor's makes have different syntax, features, etc.
Posted: Sun Mar 11, 2007 7:21 am
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?
Posted: Sun Mar 11, 2007 9:54 am
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.
Posted: Sun Mar 11, 2007 11:33 pm
by Solar
The tutorial in the wiki assumes GNU make. The Makefile in question was created on Cygwin.