Friday, March 25, 2011

Compiling on the Wii: a Lesson in Makefile

Makefiles are files used to make compiling easier on Unix like systems.
Now I know what you’re saying. You’re saying, “Centraldogma, what does the Wii have to do with Make files?”. Well, because I learned it through trying to make some Wii homebrews, so that’s how I’m teaching it. Sure I learned some basics in college, but not the level you’ll see in Open Source.
But, let’s start with the basics. Here is a basic makefile:
all: hello

hello: main.o factorial.o hello.o
        gcc main.o hello.o -o hello

main.o: main.c
        gcc -c main.c

hello.o: hello.c
        gcc -c hello.c

clean:
        rm -rf *o hello

Each has word with a colon after is a rule. all is a default rule, if you create a file called makefile and say “make” the all rule will be executed.
Immediately after the colon is the requirement for the rule to be used, if these are other rules, those other rules will be executed. In this case all requires hello. hello requires main.o and factorial.o. For main.o a file called main.c must exist in the current directory and must be compileable. The same for hello.o and hello.c.
So, all you have to do is have your main.c and hello.c with your make file and type “make” and you get the hello binary. If you want to just make an intermediary, say main.o, you would say “make main.o”.
But lets say your all done, but you want to tar everything up and send it to your friend/partnet/employer/anyone-who-will-look-at-your-code. You have all these nasty binaries and intermediaries all over your directory! Well, we have this other rule, clean, which removes any file that ends with .o and the hello binary. Just say “make clean” and it’s done.

Well, that’s the basics, on to the advanced stuff. That’s where the guys at CodeMii come in, I’ll let them explain it:
A makefile is usually found that the root directory of the source code you have. Take the gamecube template source code example (C:\devkitPro\examples\gamecube\template). In that directory you will see a file name “Makefile”. If you open it up with notepad, you’ll be able to see the different parts of a makefile.
The important parts of a makefile for this tutorial are the following lines:
1.include $(DEVKITPPC)/gamecube_rules
By specificing “gamecube_rules” we are telling our compiler that the source code we want to compile will be run on the gamecube. You would use “wii_rules” to compile for the Wii.
By changing between these two, you are changing which libraries will be used when compiling the source code. Libraries are a bunch of files which we use in our source code to interact with the gamecube or Wii system.
1.SOURCES        :=    source
2.DATA        :=    data
3.INCLUDES    :=
These lines tell the compiler which files should be compiled. Most of the time you can leave this alone as our source code will be in /source and our additional files like images, music, etc will be in /data.
1.LIBS    :=    -logc –lm
This is a very important line as it tells the compiler which additional libraries we wish to use. Say we want to play an mp3 and have the relevant code in our source to do so. If we were to compile our source with the above line, the compiler would complain and say that it can’t find the functions we are using to play an mp3 file. For playing mp3 files, you need to include the “lmad” library.
The LIBS line when we add the lmad library looks like:
1.LIBS    :=    -lmad -logc –lm
The order of how you include your libraries is also important as some libraries may reference other libraries and if you haven’t got them in the right order, the compiler will complain about it.

No comments:

Post a Comment