The compiling is the changing of the source code of a computer game into machine code. Game programmers write source code to program game computations and control flow. They use programming languages such as C++. A computer program called the compiler changes the source code into a binary. The target platform (such as a computer or console) is able to run the binary.
Generally, only game developers run the compiler. However, other users might need to run the compiler because they downloaded the latest code of a free, open source game, or they run a platform such as Unix which has several different kinds of machine code.
Compiling on Unix
A Unix game must be recompiled for every combination of operating system and processor architecture. (The operating system is the software that controls the computer, and the processor architecture is the specific type of CPU in the computer, such as x86, Alpha, or PowerPC.)
Linux/x86 is only the most common form of Unix. A binary from one Linux/x86 system will run on all of them (if the compiling user is careful and unless the other computer needs an upgrade or downgrade). This binary will not work on FreeBSD/x86, Linux/PowerPC, or OpenBSD/PowerPC. Actually, FreeBSD can emulate Linux so FreeBSD/x86 can run Linux/x86 binaries.
Thus the different Unix vendors do much compiling themselves. For example, OpenBSD/PowerPC provides on their own FTP site the binaries for several games, including NetHack and Battle for Wesnoth. Unfortunately, these games are often old versions. Also, many commercial games are only available for Linux/x86, while free, open source games can be recompiled on different Unix platforms.
Typically, one follows the four steps to compile a Unix game:
- Install dependencies
- Obtain and unpack the source code
- Configure the source tree
- Build the source tree
- Install the build
Each Unix game uses libraries. A library contains code used by several programs. Your Unix system has a C library, which contains essential code for every program. Your system probably also has the X Window System, version 11 (X11), a graphical user interface to draw windows on the screen. (If you use GNOME, KDE, xterm, or most major Linux distros, then you use X11.)
However, your system might be missing other libraries. These libraries are called dependencies because your game needs them or it will not build.
- OpenGL does 3D graphics. Most copies of X11 (XFree86 or XOrg) have OpenGL support, but it might be disabled on your system. Note that many Unix systems lack accelerated 3D so the 3D games will be too slow to play.
- SDL (Simple DirectMedia Layer) is an easy cross-platform way for games to use the keyboard, mouse, and
- SDL_mixer is an extension to SDL to provide sound.
- SDL_net is an extension to SDL to provide networking. Some multiplayer games use this to connect to the Internet.
- SDL_ttf is one way to draw text in SDL. Some games use it; some do not.
- OpenAL is a sound library.
- GTK+ is a widget library for X11. (Widgets provide buttons, menus, dialog boxes, and the like.) You already have GTK+ if you have GNOME.
- Qt is another widget library for X11. You already have Qt if you have KDE.
- Xaw is another widget library for X11, but this is part of XFree86 or XOrg, so you probably already have it.
Some games work even if a library is missing. For example, a game that needs OpenAL might still compile without it, except that there will be no music or sound (or the game might know how to use SDL_mixer instead of OpenAL).
It is a problem if you want to build a game, but it needs a library that is missing. There are ways to get libraries:
- Some games, particularly older ones, are "pure X" and need no extra libraries.
- You might already have the libraries because another game or program uses them.
- Another trick is to install an older binary of the game you want to install (such as wesnoth), and then uninstall it. The libraries might stay on your system! (This assumes that the newer version of the game wants the same libraries.)
- Otherwise, because these libraries are so common, your Unix vendor probably has them, and you can install them in the normal way (Synaptic, "pkg_add", et cetera).
Install developer packages
Some Unix vendors install libraries without their header files. These header files are never needed until you want to compile some program that uses them. So, especially on "apt" (Debian-like) or "rpm" (Fedora-like) systems, you need to install the developer packages. These are installed the same way as normal binary packages, except the packages have names like "SDL-dev".
If you run a system based on a ports tree (like Gentoo or *BSD), then their library packages include the header files. There are no separate developer packages to install.
Obtain and unpack the source code
Now you go obtain the source code. For a free, open source game, typically you visit the web site of the game, find the "Download" link, select source code, and download.
This becomes a file that is often in ".tar.gz" or ".tar.bz2" format. This means someone archived multiple files into an archive ".tar" then compressed them with gzip ".gz" or ".bz2". You might want to read Guide to UNIX/Commands/File Compression to learn how to handle these files.
Typically, you open a terminal (Guide to UNIX/Explanations/Shell Prompt), "cd" to a directory, and run commands such as:
run one of these commands $ zcat game-0.9.50.tar.gz | pax -rv $ bzcat game-0.9.50.tar.bz2 | pax -rv $ tar xvzf game-0.9.50.tar.gz $ bunzip2 game-0.9.50.tar.bz2 && tar xvf game-0.9.50.tar
This creates the "game-0.9.50" directory in the current directory. We call "game-0.9.50" a source tree because it contains game source code in a "tree" of directories. Now "cd" to it.
$ cd game-0.9.50
You might want to read files such as "README" and "INSTALL" that come with this game; you can use some "less" commands at the terminal or use some program like Nautilus or Konqueror to find these files.
Configure the source tree
Most games now use the GNU configure script to configure the source tree before the build. This script checks that all of the libraries are installed and makes configuration files so that the build will succeed on your Unix platform.
You must run the configure script:
run commands like these $ ./configure $ ./configure—prefix=/opt/game $ ./configure CC="cc -I/usr/local/include -L/usr/local/lib" $ ./configure CC="cc -I/usr/local/include/libpng"\ > CXX="c++ -I/usr/local/include/libpng"
The simple "./configure" works on many systems.
The "--prefix" option changes where the game will install. By default, most games put the program in "/usr/local/bin/name-of-game" (which is probably in your PATH of programs) and the game data in "/usr/local/share/name-of-game/"; the default prefix is "/usr/local".
You can set the CC and CXX variables to your C and C++ compilers, usually "cc" and "c++". (On systems using gcc, these are also called "gcc" and "g++".) You can specify extra options. The CC="cc -I/usr/local/include -L/usr/local/lib" setting tells the C compiler to look for header files in /usr/local/include and libraries in /usr/local/lib; on *BSD you often need to specify this. You might also have header files in a non-standard place like "/usr/local/include/libpng" (though most games use "libpng" indirectly and need no libpng header files).
When you run "configure", it makes a bunch of messages as it checks your Unix system for various features. This is necessary because different versions of different Unix variants have different features.
Build the source tree
The "configure" script made a "Makefile", which contains instructions to the Unix utility "make" on how to build the program in the correct order. The building of the program is when we actually run the compiler.
That was easy. The hard part is waiting as the C or C++ compiler works on the source code. There are a few problems that might happen:
- The build might stop with an error. You might need to run "configure" again with extra arguments. You might also need to install missing dependencies or check that your copy of the source code is not broken.
- If "make" does not work but "gmake" or "gnumake" does, then you needed GNU make to build this game.
- The computer might overheat and crash! Check that there is plenty of ventilation. Some computers, especially older and slower ones, might stress and crash anyway.
You can probably type Control-Z to suspend the make and a "fg" command to restart it, if you need to pause the build. You might want to pause so you can use your computer briefly without the build slowing your Unix system.
Install the build
This last step takes the finished program and installs it. (The instructions for your game might tell you how to run the game without installing it.)
You must become root to do this. This is unless you can install to your prefix without becoming root (for example, the prefix is inside your home directory).
do this if you have sudo configured $ sudo make install Password: ... $ or this $ su Password: # make install ... # exit $
Now it should work to "cd" to any directory (for example, plain "cd" to your home directory) and start the game.
$ cd $ name-of-game
- Wikibooks:NetHack/Building a Unix install of NetHack requires editing header files and makefiles