Compiling C++ programs with EMX the Easy Way

From EDM2
Jump to: navigation, search

By Greggory D Shaw

Introduction - Beginners Level

After I finished my college programming requirements using Java I decided to try C++ and of course I wanted to use Warp as my programming platform. Well, trying to find a starting place, I checked out the Devcorner on NetLabs. I needed 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 too many times. I finally got to compile my first three line C++ program.

Just to compile this simple program, it took about 7 files 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 set up perfectly the first time, the installation and set up should easier. So this is why I am writing this article to show you how to get a 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 is with one click and that's it. The EMX 0.9d Development Package in WarpIN format is on hobbes. 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. Some of the extra lines enable you to compile C++ programs.

Start the installation.

  1. The Readme.doc is displayed. Click next.
  2. The GNU General Public License is displayed. Click next to agree to it.
  3. Select the packages to install. I selected everything but the sources (23MB). Click next.
  4. Modify CONFIG.SYS Dialog is displayed. Click next.
  5. Installation dialog. Click next, select YES to install all packages.
  6. Add these lines to the CONFIG.SYS.
    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
  7. Reboot and you are Ready to Compile!

Using the Compiler

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

// convert.cpp
// this programs converts between Celsius & Fahrenheit
include <iostream>
int main()
{
int ftemp;
int ctemp;
int select = 0;
//***** Prints instr. & inputs your selection *******************
cout << "Select conversion: " << endl;
cout << "1) Fahrenheit to Celsius" << endl;
cout << "2) Celsius to Fahrenheit" << endl << endl;
cout << "Enter: ";
cin >> select;  // input your selection
//***** Converts F to Celsius *********************************
if (select == 1)
{
cout << "\nEnter temperature 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 temperature in Celsius: ";
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 an OS/2 command line window and type: "cd cp" to change directories. Now it's time to compile convert.cpp. To do this type:

gcc convert.cpp -lstdcpp -s

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

convert

And this is displayed on the screen.

Select conversion:
1) Fahrenheit to Celsius
2) Celsius to Fahrenheit
Enter:

More Information

The next place to go after this, is the article EMX on the Desktop by James Cannon featured in the July issue of the OS/2 eZine. And the original article Installing EMX by Brooke P. Anderson. 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 though the sixth chapter compiled without problems, minus two that used a graphics library.

Conclusion

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.

Thanks to Brooke P. Anderson and James Cannon for the inspiration for this article.