FreeDOS: Installing and running DJGPP (C, C++).
"DJGPP is a complete 32-bit C/C++ development system for Intel 80386 (and higher) PCs running DOS. It includes ports of many GNU development utilities. The development tools require an 80386 or newer computer to run, as do the programs they produce. In most cases, the programs they produce can be sold commercially without license or royalties." This is how they describe the product on the DJGPP page of Delorie. You can also call it the port of the GNU compiler collection from Unix-like systems to DOS and Windows.
The following tutorial describes the installation of DJGPP 2.0.5. The DOS platform used is FreeDOS 1.3 RC5, the way to proceed on MS-DOS or other DOS systems should be similar. The tutorial only covers the basics (C and C++ development using RHIDE), without considering special libraries (like Allegro), or other GNU programming languages (FORTRAN, ADA, Pascal...) that may be installed on the top of DJGPP.
Downloading DJGPP.
DJGPP is a really huge project, composed of a large number of packages for different platforms. Which packages (individual .zip files) you need depends on what you intend to use the software for. Browsing the files and manually choosing what to install would be rather hard, I guess. Fortunately, Delorie offers a lots simpler way to find what you need. On their DJGPP Zip File Picker page, you have the possibility to fill out a form and, based on your selections, the web application will generate a page with the links to the corresponding .zip files. In the first part of the form, you can choose if you want to build or only to run DJGPP applications (obviously to build them in our case), choose the operating system (DOS in our case), choose if you want to download the documentation files and select which programming languages you want to be installed: C, C++, Objective C (and Assembler) in my case.
Further down on the form, you can select an editor/IDE (RHIDE, similar to Borland's IDE works fine), choose if you want the text-mode GNU debugger (actually not needed if you install RHIDE that includes a debugger) and make your selections for the extra stuff shown on the page (note, that the components shown are only the most popular ones; to view them all, visit the download site itself). I selected all of them, except the sources (I did not yet try out anything of these, so I can't say how well or not they work).
The following screenshot shows the packages (.zip files) that the file picker application has selected for me. Note that files with a name ending in -b are binaries; those ending in -d are documentation.
Download the files and (the simplest way to proceed, I would say) burn them onto a CDROM (aka make an ISO to be used in your virtualization software or virtual CD-drive) in order to access them on your FreeDOS machine.
Installing DJGPP.
When unzipping the different packages, please be aware of the following:
- Unzip all files to the same directory (e.g. C:\DJGPP), conserving the folder structure as is within the ZIP archive.
- As it seems, the GNU C compilers only work correctly when short (8 characters) file and folder names are used. I'm not sure if unzip.exe included with FreeDOS would unpack the files as it should here. Using the 8-characters-filenames unzip utility from the DISLIN website is probably the better way to do; download it and add it to the ISO with the DJGPP files.
The screenshot shows the content of my CDROM (mounted as drive letter F:).
Create the directory where DJGPP should be installed and copy the "readme" and "copying" files to this directory. Copy the Dislin unzip
utility to your FreeDOS executable directory, renaming it (for example to unzip8.exe) in order not to overwrite the FreeDOS unzip utility.
mkdir c:\djgpp
cd c:\djgpp
f:
copy unzip.exe c:\freedos\bin\unzip8.exe
Using a text editor (on FreeDOS or on the host of your VMs), create a batch file including the commands to unzip all packages on the CDROM to
the DJGPP installation directory (you can extract the packages one by one, if you prefer). Here the content of my unzipall.bat:
c:
cd \djgpp
unzip8 f:\djdev205.zip
unzip8 f:\faq230b.zip
unzip8 f:\pakk023b.zip
unzip8 f:\rhid15ab.zip
unzip8 f:bnu2351b.zip
unzip8 f:\gcc930b.zip
unzip8 f:\gcc930d.zip
unzip8 f:\gdb801b.zip
unzip8 f:\gdb801d.zip
unzip8 f:\gpp930b.zip
unzip8 f:\mak44b.zip
unzip8 f:\objc930b.zip
unzip8 f:\csdpmi7b.zip
unzip8 f:\all422a*.zip
unzip8 f:\all422b*.zip
unzip8 f:\grx249s.zip
unzip8 f:\pdcur39a.zip
The archives on the CDROM (drive F:) are unpacked to the current directory (actually C:\DJGPP).
You should change the settings in FDCONFIG.SYS: Setting the number of files to 40 and the fcbs to 40,0 (if the directives don't exist,
add them, otherwise change them; if there are higher values in your file, let these untouched); setting the value of the /E parameter of the SHELL directive to 2048
(normally, you'll have to replace /E:1024 by /E:2048). Here these directives in my FDCONFIG.SYS (the SHELL directive in your file may be somewhat different):
!FILES=40
!FCBS=40,0
SHELLHIGH=C:\FreeDOS\BIN\COMMAND.COM C:\FreeDOS\BIN /E:2048 /P=C:\FDAUTO.BAT
You also have to add C:\DJGPP\BIN to the PATH, and create the new environment variable DJGPP set to
C:\DJGPP\DJGPP.ENV. If you often work with DJGPP, you can include these settings in FDAUTO.BAT. Otherwise creating a separate batch file is better practice. The
commands normally are as follows:
SET PATH=C:\DJGPP\BIN;%PATH%
SET DJGPP=C:\DJGPP\DJGPP.ENV
Testing the C compiler.
We can now perform a quick test of the DJGPP installation by trying to build a simple "Hello World" C program.
Here the sample source code:
/* Hello World */
#include <stdio.h>
int main(void) {
printf("\nH E L L O W O R L D !\n\n");
return 0;
}
The GNU C compiler is called gcc and may be invoked with just 2 parameters: 1. the name of the C source file, 2. the name of the executable
to create preceded by "-o" (without the quotes; the "o" meaning "output"). In our case (supposed that we actually are in the directory where hello.c is located and that
we want to have the executable there, too):
gcc hello.c -o hello.exe
The screenshots below show the build command (on the left) and the execution of the program (on the right).
Using RHIDE as DJGPP IDE.
The Delorie DJGPP packages for DOS include RHIDE, an IDE (editor plus debugger) for DJGPP. If you unzipped all packages shown in the directory listing of my CDROM containing the downloads at the beginning of the tutorial, RHIDE is installed and ready to be used. You can start it by simply typing "rhide" (without the quotes). To note, however, that to make RHIDE work correctly, you'll have to specify a project name as command line argument (cf. below). The screenshot shows the first start-up of the IDE.
The important point is that RHIDE works with projects, not with individual source files. If you start the IDE without specifying s project name and open for example a C source file, then this file is added to a default project, as will be all subsequently opened files. There is no problem with the first file; building it produces an executable, just as it would with a file-oriented IDE. However, if you have built some sources before (even though when these aren't actually open in RHIDE), building the source file(s) in the editor will build all these sources (because they have been added to the default project). This still works fine but doesn't make any sense, because you rebuild programs that you have already built before. However, there also is a serious problem if you try to work in a file-based (instead of a project-based) way: If one of the sources, that you built before, isn't any longer available (because you did delete or rename it), the build will fail. In fact, RHIDE tries to build all source files being part of the project, and with one of these files missing, the build will abort with an error window similar to the one shown on the screenshot below, that shows a situation, where I first built hello.c, later on deleted this file, and when trying to build hello2.cpp, hello.c (that had been added to the default project) could not be found.
Lets now create a RHIDE project called hello1 and that we will use to build our "Hello World" C program from before, using the IDE. Supposing
that the DJGPP bin directory is part of our path, we start RHIDE, specifying the project name as command line
argument:
rhide hello1
The IDE starts up with an empty project window displayed (screenshot on the left). To add the source file hello.c to the hello1 project, hit the INSERT key on the keyboard (or click Add with the mouse). A list with the files in the current directory opens (screenshot on the right). Use the TAB key to jump to this list and then the up and down arrows to navigate to the file that you want to add to the project. With hello.c selected, hit ENTER (or push the OK button).
Push the Cancel button to close the Add Item window. In the main project window, hello.c is now listed (replacing the information text <empty>) as file being part of this project. Select the hello.c item and hit ENTER: the source code of hello.c will be displayed in the editor window. Use the menu command Compile > Build all to build the project, in this case the only file hello.c (plus the libraries needed).
When the build is done, the message window opens at the bottom of the screen. If the final build result shows "no error" then all is ok and the executable has been created (screenshot on the left). If the source contains erroneous code, or something goes wrong during linkage, some error messages will be displayed. We can test the program using the menu command Run > Run. If all works correctly (i.e. if there isn't any runtime error), the message "Program exit code: 0" will pop up (screenshot on the right).
The screenshot on the left shows the content of my DJGPP sources directory, after I successfully built the C project hello1 and the C++ project hello2. Beside the sources (hello.c and hello2.cpp), the object files (output of the compiler, input to the linker; hello.o and hello2.o) and the executables (hello1.exe and hello2.exe), you can see the project files (hello1.gdt, hello1.gpr and hello2.gdt, hello2.gpr); for details about these files, please have a look at the RHIDE documentation. There are two ways to open a project in RHIDE:
- From the command line, running rhide <existing-project-name>.
- In RHIDE, using the menu command Project > Open project (screenshot on the right).
We saw above that when we run a program and all works correctly, the message "Program exit code: 0" will pop up. But, how can we do to actually have a look at the program output? One possibility is to use the menu command Window > User Screen (screenshot on the left). This opens STDOUT with the last screen output that was made there (screenshot on the right), in our case the execution of DJGPP#.BAT, that starts RHIDE (cf. further down in the text), and the output of hello1.exe. To return to the editor window of the IDE, hit the ENTER key.
Another possibility consists in configuring RHIDE to redirect STDOUT to a window of the IDE (as is done with STDERR by default). Do do this, choose Options > Environment > Preferences and in the opening Environment Options window, select the checkbox near redirect 'stdout' (screenshot on the left). This has as consequence that after the pop-up of the "Program exit code: 0" message, the window showing the program output is automatically opened (screenshot on the right).
DJGPP not only supports C, but also C++ and you can build a C++ project with RHIDE exactly the same way as you do with a C project. Here the code of a simple
"Hello World" program in C++ (if you need an example to try it out):
#include <string>
#include <iostream>
using namespace std;
int main () {
string s1 = "Hello,";
string s2 = "World!";
cout << s1 + " " + s2 << endl;
return 0;
}
To terminate this tutorial, I want to describe how, on my FreeDOS system, I take advantage of custom batch files, a different one for each programming language, to start the development related programs. This has several advantages:
- Possibility to set the path to the program(s) needed.
- Possibility to set the programming language specific environment variables.
- Possibility to load the LFN (long file names) driver if it is required.
- Possibility to set the current directory to the folder containing the source code.
- Possibility to start the IDE or editor that I use with this given programming language.
I have created a special directory (C:\FREEDOS\BATCH) where I place all my custom batch files, and normally I name them with a number sign (#) as last character,
just to be sure that I run my batch file and not possibly a compiler or other program with that name. So, I use BP#.BAT to start the Borland IDE for Turbo Pascal,
VB#.BAT to start the Microsoft Visual Basic environment, PERL#.BAT to start SET's Editor, that I use with Perl 5. And, for DJGPP development, I created DJGPP#.BAT;
here its content:
@echo off
set PATH=C:\DJGPP\BIN;%PATH0%
set DJGPP=C:\DJGPP\DJGPP.ENV
set DEVEL=djgpp
D:
cd \DEVEL\DJGPP
if "%1"=="" goto Default
RHIDE.EXE %1
goto End
:Default
RHIDE.EXE NONAME
:End
First I add the directory that contains the DJGPP binaries to the path (PATH0 is a custom variable that I set equal to PATH in my FDAUTO.BAT), and set the DJGPP environment variable, as said at the begin of the tutorial (you may ignore my custom DEVEL environment variable). Then, I set the current directory to D:\DEVEL\DJGPP, directory that contains my DJGPP sources. And finally I start RHIDE: If the batch file has been called with a command line argument, this one is passed as project name to RHIDE (ex: to start RHIDE with the hello1 project loaded, run djgpp# hello1); if no command line parameter is specified, the IDE is started with a (normally empty) default project that I called "noname".
If you find this text helpful, please, support me and this website by signing my guestbook.