Linux Game Development Part 2
Distributable Binaries
The Solution: Steps 3-4Step 3: Build custom libraries Before you can build custom libraries, you need to download the source code. You probably know the web sites for SDL, OpenAL, Ogg/Vorbis, and any other libraries you use, so I won’t cover that here. If you don’t know, just plug it into Google and it will take you there. The source code is usually packaged as a tarball, which is a compressed archive with a .tar.gz or .tgz extension. You’ll usually find it on the download page on a particular library’s web site. After you have downloaded the source tarball for each library, extract it into a temporary directory where you can build it. Now we need to use the command line. Open an Xterm or Terminal window, and cd to the temporary directory that contains the source code. You need to be in the top-level directory of the source code tree you just extracted. To build a custom version of each library, you should follow the directions that come with the library. Usually there is a README file that explains the steps you need to take. Basically, each library follows these three easy steps (you type these in on the command line): ./configureMost open source libraries come with a utility called “configure” that allows it to figure out where the development tools and libraries are installed on your system. The configure tool also provides a plethora of options you can use to control how the library is built and tailor it to your needs. To find all the options, type: ./configure --helpThere are too many options to cover for each library, so instead, I’ve decided to share with you the options I used and why I used them. Hopefully, that will provide some criteria that you can use to tailor your own libraries. For SDL, these are the options I supplied to “configure”: “--prefix=/home/troy/Projects/SDL” - specifies where my custom version of SDL will be installed at the end. All of my Makefiles (or my IDE project) will point here to pick up include files and libraries.As of v1.04, Dirk Dashing uses SDL_mixer for its audio. For SDL_mixer, these are the options I supplied to “configure”: “--disable-music-mod” – disables support for MOD, which my game doesn’t use, thereby reducing the library file size.For libogg, these are the options I supplied to “configure”: “--prefix=/home/troy/Projects/ogg” - specifies where my custom version of the library will be installed at the end.For libvorbis, these are the options I supplied to “configure”: “--prefix=/home/troy/Projects/vorbis” - specifies where my custom version of the library will be installed at the end.Dirk Dashing v1.03 and earlier versions used OpenAL for the audio. For OpenAL, these are the options I specified to “configure”: “--prefix=/home/troy/Projects/openal” - specifies where my custom version of the library will be installed at the end.For freealut, these are the options I specified to “configure”: “--prefix=/home/troy/Projects/freealut” - specifies where my custom version of the library will be installed at the end.Building a custom library is a fairly easy process. The configure utility makes it very easy, once you identify the options you need to use. Sometimes the configure script will fail because it cannot find a particular tool or library it needs. When that happens, use the package manager for your distribution to install the missing tool or library. Most good distributions provide a package manager that allows you to browse the set of available packages and search for a particular package you need. You just need to find the missing package, install it, and then run the configure tool again. Just for fun, let’s run objdump on my custom OpenAL library and compare it to the dependency list we had before: NEEDED libm.so.6 NEEDED libdl.so.2 NEEDED libpthread.so.0 NEEDED libc.so.6Wow, we’ve gone from fourteen dependencies to only four! And they are all low-level libraries that I don’t need to worry about. I found this to be true with all of the libraries that my Dirk Dashing executable directly depended on – when I built custom versions of those libraries to minimize the dependencies, I didn’t have to build anything that those libraries in turn depended on. I was able to minimize the dependencies of each custom library to the point that it only needed low-level libraries. Step 4: Use Your Custom Libraries Finally, edit the include and library paths in your Makefiles or your IDE projects to point to your custom libraries, and rebuild your application. Congratulations! You now have an application and a set of custom libraries that you can distribute that will run on almost any Linux distribution. To run your program, it will need to be able to find your custom libraries. Shared objects are not like Windows DLLs - you can’t just co-locate your libraries in the same directory as your executable and expect the executable to find them. The runtime linker in Linux only looks for libraries based on directories specified in the LD_LIBRARY_PATH environment variable. I recommend writing a short script to run your game. The script should set the LD_LIBRARY_PATH to point to your custom libraries and any system libraries you need, and then launch your executable. You can also have it cd to your installation directory first, so that the working directory is the directory where the executable resides - that way, your program can easily find all the game assets. Your desktop and menu shortcuts would then invoke your script rather than your executable. Here is an example startup script that I used during development of Dirk Dashing: # Set the library path to point to my custom libs and my game libs # Separate multiple paths with a colon export LD_LIBRARY_PATH=/home/troy/Projects/DirkDashing/Lib:. # cd to the working directory, so it can find game assets cd /home/troy/Projects/DirkDashing # Start the game ./dirkdashingThe # sign at the start of a line indicates a comment. Notice that you can set multiple directories in LD_LIBRARY_PATH by separating them with a colon. I added the “.” at the end of the LD_LIBRARY_PATH to find game libraries that are located in the same directory as the executable. SummaryThis article was rather lengthy, but here’s the whole thing in a nutshell: to build a binary executable that will run on almost any Linux distribution, you need to statically link as many libraries as possible, and build custom versions of the rest. Then you provide your application and custom libraries together in your installer. But we’ll talk about installers next time. Until then!
|