Makefile: Difference between revisions

1,788 bytes added ,  13 years ago
Added "What Is A" and a basic example to aid complete beginners to makefiles
[unchecked revision][unchecked revision]
(→‎Dependencies, pt. 3: See discussion page.)
(Added "What Is A" and a basic example to aid complete beginners to makefiles)
Line 1:
{{Rating|1}}
 
= What is a Makefile? Tutorial=
A Makefile is a file which controls the 'make' command. The 'make' command is available with most C toolchains (such as GCC) and is used to aid in build process of a project. Once the makefile has been written for a project, the 'make' command can easily and efficiently build your project and create the required output file(s).
 
Make can read dependancy information from your makefile and ensure that files are built in the correct order, and can even detect when source files or other dependancies have been updated, and can perform only the build steps that are required.
 
Your makefile can also contain other commands (or "targets") to remove built files (usually referred to as 'clean') or to create an archive of your project, ready for release.
 
= Makefile Tutorial =
Setting up a simple Makefile is easy. You can even go a long way ''without'' a Makefile, simply by using make's internal rulesets, however setting up a global project Makefile that works as intended is quite a bit harder.
 
Line 30 ⟶ 37:
 
I won't go into further details. This is not a man page, but a tutorial, so I will show you how a makefile is build, and the ideas behind each line.
 
== Basic Example ==
 
Lets say you currently build your OS (or other project) using a batch file that looks like this:
<pre>
gcc -c main.c -o main.o -Wall
gcc -c ports.c -o ports.o -Wall
ld -o kernel.o main.o ports.o -i -e _main -Ttext 0x7E00
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
</pre>
 
You can start out into the world of makefiles by creating a file named "Makefile" that contains the following:
 
<pre>
all:
gcc -c main.c -o main.o -Wall
gcc -c ports.c -o ports.o -Wall
ld -o kernel.o main.o ports.o -e _main -Ttext 0x7E00
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
</pre>
 
Here we are creating a single rule to build a target called "all" that has no dependencies, and providing the list of commands to build this target. With this file in your source directory, executing the command "make" will then find this file, and compile your OS (or other project). (Note specifically that all the commands must start with a single tab character, not spaces.)
 
== Special macros ==
33

edits