Game Development Reference
In-Depth Information
This line deines a variable with a list of compiler command-line parameters. In our
case, we instruct the compiler to search the
src
directory for header iles. If the
library source code spans across many directories, we need to add the
-I
switch
for each of the directories.
2. Next, we add the following lines for each source ile:
<SourceFile>.o:
gcc $(CFLAGS) -c <SourceFile>.cpp -o <SourceFile>.o
<SourceFile>
should be replaced by the actual name of the
.cpp
source ile,
and these lines should be written for each of the source iles.
3. Now, we add the list of object iles:
ObjectFiles = <SourceFile1>.o <SourceFile2>.o ...
4.
Finally, we write the target for our library:
<LibraryName>:
ar -rvs <LibraryName>.a $(ObjectList)
Every line in the makefile, except the empty lines and the names of the
targets, should start with a tabulation character.
5.
To build the library, invoke the following command:
>make <LibraryName>.a
When using the library in our programs, we pass the
LibraryName.a
ile as
a parameter to
gcc
.
How it works...
Makeiles consist of targets similar to subroutines in programming languages, and usually
each target results in an object ile being generated. For example, we have seen that each
source ile of the library gets compiled into the corresponding object ile.
Target names may include the ile name pattern to avoid copying and pasting, but in the
simplest case, we just list all the source iles and duplicate those lines replacing
SourceFile
with the appropriate ile names. The
-c
switch after the
gcc
command is the option to
compile the source ile and
-o
speciies the name of the output object ile. The
$(CFLAGS)
symbol denotes the substitution of the value of the
CFLAGS
variable to the command line.
The GCC toolchain for Windows includes the
AR
tool, which is an abbreviation for the archiver.
Makeiles for our libraries invoke this tool to create a static version of the library. This is done
in the last lines of the makeile.