Compiling C++ programs with EMX


Introduction

Beginners Level

(Note: The original idea came from Brooke P. Anderson and James Cannon.)


How to compile C++ programs with EMX the easy way! After I finished my college programming requirements using Java. I decided to try C++ and course I wanted to use Warp as my programming platform. Well trying to find a starting place, I checked out the Devcorner on NetLabs, needing a compiler. The section on compilers included information about C/C++, Pascal, and Java. I ended up picking EMX GCC for OS/2, it's the port of the GNU C/C++ compiler.


Well after many hours of downloading files, reading through the documentation and editing my config.sys file to many times. I finally got to compile my first three line C++ program.


Just to compile this simple program, it took about 7 files needed and 23 optional files to use the GNU C/C++ compiler with emx. I could have been missing something, but even if I got everything setup perfectly the first time. The installation and setup should easier, so this is why I am writing this article to get quick and easy programming environment. So if you decide to throw your C++ book out the window half way through the second chapter, you are not out hours of work.

How to get EMX

The easy way one click and that's it. The EMX 0.9d Development Package in WarpIN format is on hobbes. Click here! The source is unknown and I do not know how complete it is, but it works for what we need it to do. Plus it can be easily uninstalled by WarpIN.

How to Install EMX

The following describes installing EMX and adding additional lines to your CONFIG.SYS, that the WarpIN package fails to do. The extra lines enables you to compile C++ programs.

Start the installation.

  1. The Readme.doc is displayed. Click next.
  2. The GNU General Public Licnese is displayed. Click next to agree to it.
  3. Select the packages to install. I selected everything (23MB), but the sources. Click next.
  4. Modify Config.sys Dialog is diplayed. Click next.
  5. Installation dialog. Click next, select YES to install all packages.
  6. Add these lines to the CONFIG.SYS.

  7. SET CPLUS_INCLUDE_PATH=c:/emx/include/cpp;c:/emx/include
    SET PROTODIR=c:/emx/include/cpp/gen
    SET OBJC_INCLUDE_PATH=c:/emx/include
    SET TERM=ansi
    SET TERMCAP=c:/emx/etc/termcap.dat

  8. Reboot and you are Ready to Compile!

Using the Compiler

Time to start writing a C++ program, that we can compile. Fired up the System Editor.

// convert.cpp
// this prgrams converts between Celcuis & Fahrenheit
#include <iostream>
int main()
{
int ftemp;
int ctemp;
int select = 0;
cout << "Select conversion: " << endl;
cout << "1) Fahrenheit" << endl;
cout << "2) Celsius" << endl << endl;
cout << "Enter: ";
cin >> select;     // input your selection
//***** Converts F to Celcuis *********************************
if (select == 1)
{ 
cout << "\nEnter temperture in fahrenheit: "; cin >> ftemp; ctemp = (ftemp-32) * 5 / 9; cout << "Equivalent in Celsius is: " << ctemp << endl; } //End of IF //****** Converts C to Fahrenheit **************************** else if (select == 2) { cout <<"\nEnter temperture in Celcius: "; cin >> ctemp; ftemp = ctemp*9/5 + 32; cout << "Equivalent in Fahrenheit is: " << ftemp << endl; } //End of IF //***** Prints message if invalid choice is made *********** else cout << "Valid options 1 or 2." << endl; return 0; } // End of Main

Okay short and simple, but it will compile. Now you can save it to any directory. I save my files to c:\cp to keep all my programs together. Open up a OS/2 command line window and type CD CP to change directories. Now its time to compile convert.cpp type:

     gcc convert.cpp -lstdcpp -s

That's it! Now we can run the program.

     convert

And this is diplayed on the screen.

     Select conversion:
1) Fahrenheit
2) Celsius

Enter:

More Information

The next starting point after this, is the article EMX on the Desktop by James Cannon featured in the July issue of OS/2 eZine. There are many C++ tutorials on the Internet, the cplusplus.com tutorial is one, and there is a link at the bottom of the page on compiling console programs with different compilers (Windows,Linux,Unix). The book Object-Oriented Programming in C++, 3rd Ed. by Robert Lafore is what I am using. Every program that I tried thourgh the sixth chapter compiled without problems, minus two that used a graphics library.

Conclusions

I hope that this will save you some time on setting up your C++ programming environment. So you can jump right into programming. And try at least to get through the third chapter of your C++ book, because the first two chapters put me to sleep too.