James Fowler's DJGPP Tutorial

By James Fowler, Jr.
Undergraduate Teaching Assistant
CS 222 & CS 262
George Mason University
Computer Science Department

I. Download

1. Download my DJGPP Windows installer package from Dr. Nordstrom's web site at: http://cs.gmu.edu/~dnord/DJGPP.exe

II. Install

1. Install using the "Full Installation" option or, if disk space is a concern for you, unselect just the "C++ Compiler" checkbox to only install the C compiler, not C++.

2. Check "Create a desktop icon" later in the installer if you want desktop icons to be installed instead of just Start Menu icons.

3. The installer will install the DJGPP software in a folder named "djgpp" at the top level of the disk drive where Windows resides. For most people, this is the "C:\djgpp" folder. The rest of this tutorial will assume the C: drive. If Windows is installed on another drive on your system, please replace C: with the correct drive letter (e.g., D:).

4. An important subfolder is the "C:\djgpp\home" folder. This is the folder where the code you want to compile should be placed in for ease of use.

5. Unless you changed the Start Menu folder location from its default of "DJGPP" during installation, there are three shortcuts in the "DJGPP" Start Menu folder: "DJGPP," "DJGPP Home Folder," and "Uninstall DJGPP."

6. The "DJGPP" shortcut will open a DOS command prompt window, properly set up the required path and environment variables, and change the working folder to the "C:\djgpp\home" folder.

7. The "DJGPP Home Folder" shortcut will open the "C:\djgpp\home" folder in an Explorer window.

8. The "Uninstall DJGPP" shortcut will uninstall DJGPP from your system. The "C:\djgpp\home" folder will not be removed unless it is completely empty.

9. If you chose to create Desktop icons during the installation process, there will be shortcuts on your Desktop equivalent to the "DJGPP" and "DJGPP Home Folder" Start Menu shortcuts.

III. Simple C Compilation

1. To compile a simple C program using DJGPP, place the file in the "C:\djgpp\home" folder. You can either save the file directly into this folder or cut or copy and paste it into an Explorer window you opened by clicking a "DJGPP Home Folder" shortcut. For this example, we will use "C:\djgpp\home\prog.c" as a filename for a self-contained C program.

2. Click a "DJGPP" shortcut to open a DOS command prompt window. You are in the "C:\djgpp\home" folder.

3. Type "gcc -o prog.exe prog.c" at the "C:\djgpp\home>" prompt and hit Enter. The "gcc" in the command is for GCC, the GNU C Compiler. The "-o prog.exe" part tells the compiler to store the output executable program in a file called "prog.exe" that can be run in a DOS command prompt window. Replace "prog.exe" with whatever ".exe" filename that you want to call your output program. The "prog.c" part is the name of your C source file. Replace "prog.c" with the name of your C source file. Note: your ".c" and ".exe" files do not need to have the same base filename like in this example. If there are errors or warnings, you will see them now, otherwise you will see:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\djgpp\home>gcc -o prog.exe prog.c

C:\djgpp\home>

4. To run your program (if there were no errors), type the name of the program you specified after the "-o" switch in the previous step without the ".exe" extension at the "C:\djgpp\home>" prompt and hit Enter.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\djgpp\home>gcc -o prog.exe prog.c

C:\djgpp\home>prog

IV. Intermediate C Compilation

1. The previous example compiled and linked in one step to produce an executable program. Sometimes, you need to compile many parts separately and link them all together into a single executable.

2. For this example, let's say you have three files in your "C:\djgpp\home" folder. A file named "functions.h" is a C header file with prototypes for functions that you will use in a C source code file named "main.c" which contains your "main()" function. A file named "functions.c" is a C source code file that implements the functions declared in the "functions.h" C header file.

3. If one isn't still open, open a DOS command prompt window by clicking a "DJGPP" shortcut.

4. Type "gcc -o functions.o -c function.c" at the "C:\djgpp\home>" prompt and hit Enter. The "-o functions.o" part tells GCC to store the output object file in a ".o" file named "functions.o" to be linked into an executable file in a later step. The "-c function.c" part tells GCC to compile the "functions.c" C source code file but not link the file right now. Again, only the ".o" and ".c" file extensions are important; you can change the base filenames to whatever you want for each as long as you also change the filenames for the rest of the steps in this example.

5. Next, do the same for the "main.c" C source code file. Type "gcc -o main.o -c main.c" at the "C:\djgpp\home>" prompt and hit Enter.

6. You now have two compiled object files that need to be linked together into a single executable file. Type "gcc -o prog.exe main.o functions.o" and hit Enter to link the "main.o" and "functions.o" object files into a single "prog.exe" executable file. The "-o prog.exe" part tells GCC to store the output executable program into the "prog.exe" file. The "main.o functions.o" part is a list of all of the compiled object files to link together into the "prog.exe" executable file. If you had more object files to link, you would add them to this list.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\djgpp\home>gcc -o functions.o -c functions.c

C:\djgpp\home>gcc -o main.o -c main.c

C:\djgpp\home>gcc -o prog.exe main.o functions.o

C:\djgpp\home>

7. If you used functions from a library such as "libm," the C Math library ("#include <math.h>"), you need to modify the above instructions slightly.

8. For example, let's suppose that some of the functions in the "functions.h" and "functions.c" files call functions from the C Math library to calculate cosines, sines, or other operations.

9. You would then need to link the C Math library with the other object files to produce the "prog.exe" executable file. Replace the "gcc -o prog.exe main.o functions.o" command from above with "gcc -o prog.exe main.o functions.o -lm" to do this. The additional "-lm" part tells GCC to link with the "libm" C Math library bundled with DJGPP. The "lib" prefix of "libm" must be omitted.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\djgpp\home>gcc -o functions.o -c functions.c

C:\djgpp\home>gcc -o main.o -c main.c

C:\djgpp\home>gcc -o prog.exe main.o functions.o -lm

C:\djgpp\home>

10. To run the executable program from either example, type the program name without the ".exe" extension at the "C:\djgpp\home>" prompt and hit Enter. For these examples, "prog" should be typed.

V. More Advanced C Compilation Using Make

1. A better way to perform the compilation and linking steps from the "Intermediate C Compilation" section of this tutorial is to automate it using Make.

2. First, you must create a file named "makefile" and place it in the "C:\djgpp\home" folder. This file must be named "makefile" or else these instructions won't work. Below is a makefile for the example from the previous section using the C Math library.

C:\djgpp\home\makefile
# Section 1
all: prog.exe

# Section 2
prog.exe: main.o functions.o
<TAB>gcc -o prog.exe main.o functions.o -lm
# Section 3
main.o: main.c
<TAB>gcc -o main.o -c main.c
# Section 4
functions.o: functions.c functions.h
<TAB>gcc -o functions.o -c functions.c
# Section 5
clean:
<TAB>del *.o

3. All of the lines in a makefile must start in the leftmost column of the file; there can be no preceding spaces.

4. A line that begins with a "#" (number sign) is a comment line and will be ignored by the Make program. In the example file, I have created five comment lines to label the five sections that I will be discussing in this example. Comments are optional, but it is good style to include them to make your code more readable. Empty lines are also permitted to make the file more readable.

5. Each section in a makefile uses two lines. We will see later why Section 1 appears to violate this rule and why it really does not because the second line is blank, but still is there.

6. The first line in each section is in the "<target name>: [list of dependencies]" format. For Sections 2 through 4, we see that "<target name>" is the name of the output file after the "-o" in the second line of the section. This is the way a normal section is structured. Sections 1 and 5 are special types of sections that will be discussed later. For a normal section, the "<target name>" is followed immediately by a colon and then a space. There is no space before the colon, only after it. After this, there is a list of dependencies. Dependencies are names of files that will cause the command on the next line to be run if they have been modified.

7. Make will only run the command on the second line of a section if a file named "<target name>" does not exist or is older than any of the files in the list of dependencies. This speeds up recompilation because it only recompiles the files that need to be recompiled because their source code has changed.

8. The second line in a normal section is a command to be executed on the command line if Make determines that one of the two conditions for recompilation has been met. The line must start with a Tab character and cannot have any spaces before or after it. The command to be executed on the command line is immediately after the Tab character.

9. In Section 4 of the "makefile" file above, you can see that the "<target name>" is "functions.o" because the "gcc -o functions.o -c functions.c" command on the next line will produce this file. The dependencies are "functions.c" and "functions.h" because the "functions.o" file will need to be recompiled if either of these files change. These two files are the C source and header files that are used to compile the "functions.o" object file.

10. In Section 3, the "<target name>" is "main.o" because the "gcc -o main.o -c main.c" command on the next line will produce this file as its output. The sole dependency is "main.c" because this is the C source code file for the "main.o" object file.

11. In Section 2, the "<target name>" is "prog.exe" because the "gcc -o prog.exe main.o functions.o -lm" command on the next line will produce this executable as its output. Remember, the "-lm" part links to the C Math library and is unnecessary if you are not using the C Math library. The dependencies are "main.o" and "functions.o" because the command on the second line links these two object files into the "prog.exe" executable file. Make will recursively check the dependencies of both of these dependencies to see if these object files need to be recompiled before linking them into the "prog.exe" executable. It will perform this recursive check by going into Sections 3 and 4 and checking their dependencies and running their commands if necessary.

12. Section 1 is a special type of "<target name>," a dummy target. Since there is no "all" filename and none of the sections produce this file, it will always go on to the dependency of "prog.exe" as if that was the "<target name>." Since the second line is blank, no commands will be executed. This is a special "<target name>" that will be called when "make" is run on the command line with no arguments.

13. Section 5 is another dummy "<target name>," but it is lacking any dependencies. It will always be run when "make clean" is run on the command line. This command will remove all of the ".o" object files in the directory since they are intermediate files that are not needed after the executable has been produced.

14. To run Make on the command line to make everything (but not run "make clean"), type "make" and hit Enter. You can also jump to any of the "<target name>" sections by typing "make <target name>" at the command prompt opened using a "DJGPP" shortcut. Executing Make with no arguments is equivalent to running "make all" on the command line.

15. Running Make a second time will display "make.exe: Nothing to be done for 'all'." because everything is up to date and nothing needs to be compiled or linked.

16. Running "make clean" will delete all of the ".o" object files and now if you run "make" again it will recompile everything.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\djgpp\home>make
gcc -o main.o -c main.c
gcc -o functions.o -c functions.c
gcc -o prog.exe main.o functions.o -lm

C:\djgpp\home>make
make.exe: Nothing to be done for 'all'.

C:\djgpp\home>make clean
del *.o

C:\djgpp\home>make
gcc -o main.o -c main.c
gcc -o functions.o -c functions.c
gcc -o prog.exe main.o functions.o -lm

C:\djgpp\home>