Intro to Linux Development, Part 1
by Andrew Vehlies

Get the source code for this article here.

Welcome, brave soul, to the "Intro to Linux Development" series of Articles on GameDev.net! In this series of articles, I will lead you through basic Linux development of applications with a strong focus on games.

The first tutorial, the one you are viewing right now, will introduce you to many of the development tools that Linux has to offer. We will go through basic programs such as vi (a Text Editor), The GIMP (an Image Editor), and GCC (the most common compiler). Various other topics such as packaging and makefiles will also be covered.

As you probably know, most Linux programs are Open-Source. Open-Source means that the source code is freely distributable and everyone can view it. As well as being distributed as Open-Source, most Linux programs are distributed AS source code and do not give out binary files. Why, you ask? Well, certain systems have to include files into a build, while others don't. If someone compiles it on their system, their system will know exactly what it needs to include and exactly what it doesn't need.

Before we begin, let me set the prerequisites for this tutorial. I expect that you will be familiar with the C programming language. I will also assume that you know basic linux commands (rm, cd, ls). One more thing that I'll assume is that you can work your way around your Window Manager. Some popular ones are KDE, Gnome, Window Maker, and IceWM. Let's not forget BlackBox, Sawfish and Enlightenment though!

So, feel up to the challenge? Put on your Tux and Proceed to the Next page where I'll explain vi and The GIMP.

I see that you stuck with me. Good for you! Linux Development is a topic that many people get discouraged at because of how difficult it looks to compile things (we'll cover compiling on page 4). I wish we could delve into some code...but you can't type it in anything yet! So without further adieu...here is a brief introduction to vi.

vi is a powerful text editor. In fact, I'm using vi right now to write this article. vi can be used through a command-line, or it has a graphical equivalent named GVIM because you probably have VIM (Vi IMproved) installed. To access vi through a command-line, simply type "vi" without the quotes in a shell. To access GVIM, please check your K (or Gnome) Menu. My GVIM binary file is located at /usr/X11R6/bin/gvim.

Some basic commands that you probably want to know are:

i - Go Into Insert Mode (Typing)
Esc - Go to Command Mode
:cal SetSyn("c") - Sets the Syntax Coding for C. (only in GVIM, not vi)

Those are probably the basic commands that you will need to know. You can only type in the :cal SetSyn("c") script when you're in Command Mode. Command Mode doesn't allow any text editing, but it lets you change preferences in vi. If you want to type something, you must enter Insert Mode by pressing the i key. If you are in insert mode, and you want to go back to command mode, press Escape.

To save your file, go to Command Mode and type :w for Save; type :sav file for Save As. Type :e file to open an existing file. To create a new file, type :enew. To quit vi, type :qa and type :wqa to quit and save the file.

Well, that was an interesting crash course in vi, eh?

As well as that crash course in vi, I'd like to introduce you to a little program called The GIMP. GIMP (Yes it is an Acronym) stands for GNU Image Manipulation Program. Basically speaking, it's an image editor. Now, I'm not exactly an artist, but I can get you started on a bit of The GIMP's features.

The GIMP offers a lot to the artist inside of you. It offers exporting and importing many, many different file formats. Some formats include .raw, .pcx, .tga, .bmp (Yes, the Windows File Format), .jpg, and especially .PNG! Now, the PNG (Portable Network Graphics) file format is a very popular one because it has the compression, transparency, and interlacing of a GIF, but is completely free to use. If you did not know, the GIF file format is proprietary and legally requires a $5000 fee to use it. Sucks, doesn't it? But there's a plus side to this. The PNG File Format is actually much more advanced than the GIF File Format. PNGs are slowly becoming used more and more in web graphics and game graphics for their high compression level.

What you'll notice first about The GIMP are the multiple windows that make up the program. The standard windows are the main window (titled The GIMP), the Tool Options window, the Layers Window, the Brush Selection Window, and then the Image window. Now, let me brush over them (couldn't resist ;) and explain how to use them.

The Main Window has all of the tools you can choose. They're pretty self-explanatory and if they aren't, they have a tooltip if you hover over it. In the lower-left corner are your two current colors (foreground, background). To switch them, use the two arrows. When you are drawing, you will always draw with the foreground color. The Layers window is used just like layers on any other paint program. You can have various layers of an image to add things you're not sure you like yet or to try to trace something.

The Tool Options Window explains the certain options for whatever tool you chose from the Main Window. It would be too hard to explain all of them here, so I'll let you play around with them. The Brush Selection is where you'll choose the brush size and pattern. These are all really self-explanatory.

To get a new image going, go to the Main Window and click on File->New... and set the attributes to what you'd like, then click OK. To Save your Image, Right-Click on the image and go to Save As... or Save. To exit The GIMP completely, go to the main window and click the X in the upper-right hand corner. It will close all of the windows related to The GIMP.

I'm sorry if you didn't really enjoy my coverage of The GIMP. I am not a graphical genius. I only use The GIMP to make logos and makeshift graphics. My coverage was brief because I am not skilled in The GIMP. So, if anyone else is, maybe you should write a tutorial on making graphics with The GIMP!

Well, how was that page? Was it hard to swallow? Don't worry, you'll do fine. If you get confused, just read it over, it'll clear up, I promise. If it doesn't clear up, and you're having some trouble, you can contact me (find the info on the last page). May the Force be With You while you click the Next Button.

Let's be savage beasts and dive right into the code, ok? Sounds good to me.

/* linux-helloworld.c */

#include 	/* Include the Standard IO Header */

int main()
{
	char cAnswer;				/* A Character to Prompt at Exit */
	printf("Hello Fellow Linux Users!");	/* Every Program has it */
	scanf("%c",&cAnswer);			/* Make Sure the Program doesn't Quit Immediately */
	return 1;				/* Exit After it Detects the Character */
}

You'll notice a few things at first. Some of them are most likely:

1) The use of C instead of C++. Even though Linux does have C++ libraries, C is more commonly used. C is much more standardized. C is basically as standardized as you can get with a programming language (By Standardized I mean it is ANSI Compliant). Linux can handle C++ fine, it's just more common to use C because most of Unix/Linux/BSD were written in Assembly and C, thus proving it as the native language.

2) The c in cAnswer. The c stands for character (char). I hope you catch where I'm going with this...That's right! Hungarian Notation! Many Linux people do not like Hungarian Notation...I'm not really sure why. You can use it if you want, but I plan on using it in the tutorial just for clarity? Ok? Ok.

Well there you go. Now you're saying "Hey Wait! That's how I did it on Windows too!". *grin* Duh! That's just standard C. But just you wait! This article has just begun. On the next page, we'll get into compiling and after that, distributing! Fun, Fun, Fun! So please, read on, grasshopper.

"Wow, cool! My First Linux Program! But...How do I compile it?" Ahhh...I'm so glad you asked, my son (or daughter).

Linux mainly uses a compiler called "gcc" (GNU C Compiler). It is probably the most ANSI-C Compilant compilers I've ever used. Now, wanna compile your program? Go to a shell, cd to your directory and type:

gcc linux-helloworld.c

It will generate a file called a.out so, what I expect you to do next, is type ./a.out and see the results. Happy? Heh, well I was. There are many more options you can send to gcc, I will cover a few here.

-c will compile the code to an object file, not an executable.

-o will create the file as . For example, -o my_app will compile your source into an executable called my_app, which you can then call by typing ./my_app.

-l will link with a library. For SDL, -lSDL will link to the library. See other library documentations for help.

-L will tell the path that the libraries are located in. -L/usr/lib will tell gcc that they are in /usr/lib.

-O will turn optimization on and off. The level of optimization is 1 - 6. 1 is the lowest, while 6 is the highest.

-pendantic -W -Wall should be used if you want gcc to tell you about your coding (dis)abilities.

So, an example program that is compiled with SDL would look a bit like this.

gcc `sdl-config --cflags` -W -Wall -O2 program.c -o game -lSDL -lpthread

Wow! It compiled! How cool is that? Well I think it is very cool. If you're confused at all of the other parameters, then it's ok. Most of them are SDL specific and will be covered in later tutorials. But...wouldn't it really suck to type in all of that every time you want to compile? What if you had a few hundred .c files? Wouldn't it take very long? Along comes Make to the rescue!

Make is a utility that will take one set of commands stored in a file (either makefile or Makefile) and will run them. It is extremely convenient for compiling because you don't always have to type in the data. Many makefiles can get very large and will save a lot of time. Along with saving time, Make will check to see if you have updated the file since the last build. If you haven't, then it won't recompile that file. This can vastly cut down on compile time. Now, want to learn how to use Make? Sure you do. So keep reading.

Make runs on variables. As you *hopefully* know, variables are placeholders for data that can change. Variables are set by simply typing VARNAME=data. Easy, huh? Comments in Makefiles are done by using # in front of the comment. So without further adieu, let's jump into Makefiles!

FLAGS=-W -Wall -ggdb # flags to pass
# passing -On (n being a number 1-6) will optimize
# use this to make the final project
# instead of --ggdb to debug

CC=gcc # the common Linux compiler
FILES=linux-helloworld.c # only one source file
TARGET=helloworld  # what we want to compile to
demo:
	$(CC) $(FLAGS) $(FILES) -o $(TARGET)

How do you like THAT? Spiffy, eh? You'll notice the variables I set at top to hold things such as the flags to pass, the name of the compiler, the files, and the final product name. Now, you'll notice the line "demo:" and the line full of variables.

The "demo:" line tells you an argument you can pass to Make. You would call this by typing "Make demo" and then it would follow the command list that you wrote out for demo. To do this, type demo: then press return, then press Tab *once* and type the line you wish to be executed. If you'd like more commands to run, then press return and Tab and follow the pattern.

When you type "Make demo", the Make utility will fill in all the variables so what the shell really sees is:

gcc -W -Wall -ggdb linux-helloworld.c -o helloworld

It's so easy, you'll never want to work without makefiles again. You can use makefiles for almost anything. I used it to make a .tar.gz archive of this article series because I'm too lazy to keep typing the tar command. Hey, it works. So, you wanna try out the Make utility? Type the example above into a file and name it Makefile or makefile. Make sure it's in the same directory as your source code, and then cd to that directory. Then, type Make demo. If everything worked out well, then type ./helloworld -- Woohoo! You're nearly on your way to Linux development success! Now you can compile programs, "Make" them, but...how will you distribute them? Well, we're going to use a program called tar to put the files in a .tar.gz archive. .tar.gz is somewhat of the .zip file equivalent in Windows.

tar is an Archiving Utility that is commonly used on Linux and Unix systems. We will be compressing it with GZip which tar calls for high-compression ability. Let's delve right into the command line and get our hands dirty, shall we? Oh, but with two of them.

tar -cvzf tar-example.tar.gz file1.txt *.c images/

tar -xvzf tar-example.tar.gz

There's a few tasty tar lines. First, we'll go through the one on the top and its variables. The argument -c will create a new .tar.gz file (c for create). The -v argument will show you all the files that it has processed (v for verbose). -z will do the compression using gzip, a compression utility with a lot of power (z for zip). And the final argument, -f, means to use an archive file (f for file). Also, you can use -j instead of -z for compression using bzip2, another compression utility. If you use bzip2, the file will have the ending .tar.bz2

Now, we'll get to the other parts of the tar command. tar-example.tar.gz is the file you are going to archive the files into. file1.txt will add the file named file1.txt to the archive. *.c will add all files in this directory that have a .c extension to the archive. images/ will add everything in the images folder into a folder named images/ inside of the tar.gz file. Let's look at a file tree for clarity, ok?

~/files
  `images/
     `image1.png
     `image2.png
  `file1.txt
  `main.c
  `player.c


   The Directory


~/files
  `tar-example.tar.gz
     `images/
        `image1.png
        `image2.png
     `file1.txt
     `main.c
     `player.c
  `images/
     `image1.png
     `image2.png
  `file1.txt
  `main.c
  `player.c

   The Folder After using Tar

You see? It takes everything exactly how you saw it and compresses it into your .tar.gz file! It even keeps the directories in their current state instead of mixing them together! How Awesome! Now, I guess we should discuss the other tar line that I gave you above, eh?

The second tar line will extract the .tar.gz file into the current directory. The only variable that changes is -c to -x (create to extract).When you run that command, it will extract all of the files in the .tar.gz file into the directory you are located in. Let's look at another file tree for clarity.

~/files
  `tar-example.tar.gz

Before Extraction

~/files
  `images/
    `image1.png
    `image2.png
  `file1.txt
  `main.c
  `player.c
  `tar-example.tar.gz

Well there we go. Your lesson in tar is done! How did you like it? If you're not getting the grasp of it, then try it out! It will make a lot more sense once you start using it on your own. Well, now you can finally distribute your game. But, is this series really done? Read on in the next page to find out more and see my closing statements.

Finally! It's done! It's been a good week or two since I've started writing the article, but now I'm finally done. Ahh, it's been a bumpy ride, but I did it. First, I'd like to give a few closing notes about the future and then some thanks to people who have helped me along my way.

First of all, I plan to continue a series out of this article. In the next one, I will probably explain SDL enough for you to use OpenGL over it since I know a lot of you have been saying you only want a tutorial to get you started with OpenGL on Linux. So, your wish is my command, and that command will be executed.

Secondly, I need to thank a few people and...things(?). I want to thank Mike DePalatis (CmndrM) for his proofreading on my article. His insights have been helpful and he's caught a few...weird...lines in my text. Next, I'd like to thank Dave Astle and Kevin Hawkins for 1) making GameDev.net, 2) formatting this article, 3) hosting the article. I also wish to thank #linuxhelp and #sdl on irc.openprojects.net for telling me when I'm being stupid and showing me the know-how that every Linux user should have. Oh yeah! I shouldn't forget John Hall and Loki Software for writing the book Programming Linux Games. It's an excellent resource and I think anyone that really wants to get into Linux development should read it. I'd also like to thank the command man for telling me how to use things such as tar. man is a great command (stands for manual) that will tell you all it knows about a topic.

Well, I'm off and will probably start writing the next article in the series after I take a nice long nap. So, I'll leave you with a few notes about myself and my contact information in case you need help.

My name is Andrew Vehlies and I'm a computer junky. I've been using C for 2 years and C++ for about one. I've made some games in SDL but I mainly end up doing web development or helping people solve problems. I'm also a webmaster and designer for StickSoldiers and StickSoldiers2 (http://www.sticksoldiers.tk). I started my own open-source game programming project called EchoGames (http://echogames.sourceforge.net) as well as a community project called Snoballz (http://www.sourceforge.net/projects/snoballz) which is still in its design stage.

Contact Information
Email: Cheesemonk13 at hotmail dot com (please make the subject "Linux Article")
AIM: Andyrewww
MSN: uses same email as "Email" above
ICQ: 71478137
Y!im: avehlies or weezmasvelte
IRC: roy on irc.afternet.org
superRoy on irc.openprojects.net
(I suggest using Email, MSN, or IRC as I will use those the most)

Other people offering to help
Name: AJ Jones
Email: Merlin2k at pacbell dot net
IRC: Merlin2k on irc.afternet.org

Great Links
http://www.linux.org - The Home of Linux
http://www.libsdl.org - The Best Multi-Platform Library, covers Input, Sound, Window Creation, Graphics, and misc.
http://www.opengl.org - The Home of OpenGL

Is that it? Can I leave? Well, I guess there are only two things left to do.

:sav ~/Articles/part1.txt

:qa

Discuss this article in the forums


Date this article was posted to GameDev.net: 12/5/2002
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Featured Articles
Linux

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!