A Quick Introduction to C++ Programming with BeIDE

by Kevin H. Patterson
11-15-2001

Before You Begin

This tutorial assumes that you are familiar with several things. First of all, you should have a good working knowledge of computers and programming concepts. A familiarity with C or C++ (or a good book to fall back on) is a real plus. This tutorial will not attempt to teach you how to program, or anything about the C or C++ languages. It will show you how to create a terminal-based program using BeIDE for BeOS.

This tutorial is optimized for viewing with NetPositive, the lightweight web browser the comes with BeOS. Viewing this tutorial from within the Be Operating System will make things much easier.

You'll need to have BeOS (version 5 or later, preferably) installed on your PC. If you already have it, skip down. If not, you can find BeOS 5 Professional for sale in some computer software outlets, or at a number of places online. Check www.gobe.com. If you're not ready to buy, you can download the FREE BeOS 5 Personal Edition from free.be.com. It's a rather large download, but well worth it.

You also need to have the BeOS Developer Tools installed. If you already have them installed, skip down. The developer tools are included with BeOS 5 Pro, and can be selected for installation during the BeOS installation process. If you have a BeOS 5 Pro CD, and didn't install the Developer Tools, you can re-run the installer and install them without affecting your previous installation.

If you're running the BeOS 5 Personal Edition, you can download the Developer Tools from ftp://ftp.be.com/pub/beos/BeOS5-DevTools.zip.

Getting Started

If you're familiar with Microsoft Windows operating systems, the keyboard "shortcut key" that BeOS uses by default may be confusing. I recommend changing your shortcut key if this is the case. If you're used to Apple Macintosh systems, you may be comfortable with the default setting.

Now, you'll need to launch BeIDE, the BeOS Integrated Development Environment. If you have the Developer Tools installed, you can find BeIDE on your Applications Menu.

01-10-BeIDEMenu.png (17597 bytes)

If you have an icon for BeIDE on your desktop, you can double-click on that.

01-11-BeIDEIcon.png (712 bytes)

The Project Window

When BeIDE first opens, it will open with a Code Editor window. If you're just itching to start typing in code, this is fine. However, we want to create a "Project" and organize things first, before we start typing in any code. The Code Editor window looks like this:

02-Editor.png (4756 bytes)

To create a new Project, choose "New Project..." from the "File" menu.

03-NewProject.png (9639 bytes)

You'll be presented with a small window that lets you create a new project based on a number of "canned" templates. We won't discuss the details of all these various templates here; suffice it to say that the different templates mainly link your finished application with a varying selection of system libraries. Since we will be creating a simple terminal-based application in this tutorial, we'll only need the Standard Template Library. Choose BeSTL. This will start you off with the necessary minimum BeOS system libraries, plus the C++ Standard Template Library.

04-NewBeSTL.png (4468 bytes)

Make sure the "Create Folder" checkbox is checked; it will help keep things much more organized. Choose a location on your hard drive where you would like to save your newly-created project. Be sure to keep the ".proj" extension at the end of your Project filename. If you don't, you'll run the risk of overwriting your Project file with your compiled application executable later on. You don't need to save the project in a unique folder; BeIDE will automatically create a folder with the same name as your Project file, and save the project file (and other files) inside this folder.

05-CreateProject.png (9070 bytes)

Click "Save" to save your new Project. Now you will have a "Project Window", where you can collect and organize all the various files and libraries that will compose your final application. You can close the blank Code Editor window that opened when you first launched BeIDE.

06-01-ProjectWindow.png (4247 bytes)

The libraries that are listed in the Project window are automatically included from the BeSTL project template.

BeIDE Settings

If you're used to programming or development on another platform or operating system, you may want to customize some of BeIDE's settings to better suit your style.

I've included a BeIDE Customization Page, and I highly recommend that you check it out. It can make life a lot easier.

The Project Folder

As I mentioned earlier, when you create a new BeIDE Project, it creates a new folder with the same name as your Project file, and saves the Project file inside this folder. You can view the folder in the Tracker.

07-TrackerView.png (11567 bytes)

You can also view the contents of your Project folder in the tracker.

08-TrackerFolder.png (7718 bytes)

Here you can see the Project file, and another folder the BeIDE creates to store temporary linker objects. The ".cs" file is created by a small (and useful) BeIDE add-on that I use, called "DontWorry". If you don't have this, don't worry. BeHappy.

The Code Editor

To start typing code into your Project, you need to open a Code Editor window, which represents a text file. Choose "New Text" from the BeIDE "File" menu.

09-EditorWindow.png (5865 bytes)

A new Code Editor window will appear, very much like the one the opened when you first launched BeIDE. It's a good idea to save this new text file right away, so that you can conveniently use the "Save" keyboard shortcut to save your code periodically while you work. Choose "Save" from the Code Editor "File" menu.

10-SaveText.png (10243 bytes)

A Save File dialog will open; do the Right Thing and save the file in the Project folder, along with everything else.

11-SaveTextAdd.png (7621 bytes)

If you're creating a file with code, you should give it a ".cpp" extension. If you're creating a header file (we won't be), you should give it a ".h" extension. BeOS does not use or rely on the extensions that you choose, but it does help you, as a programmer, identify different files quickly by looking at their file names.

Check the "Add to Project" checkbox, and save yourself the trouble of having to drag the newly-saved file from the Tracker into your BeIDE Project window. It can be added automatically. Click "Save".

Organizing Your Project

If you did like you were told, you'll see your new file listed in the BeIDE Project window.

12-OrganizeProject.png (4494 bytes)

You should drag the file down to the bottom of the list, because it is not a library, and that is the Right Thing to do.

13-NewGroup.png (8607 bytes)

And since it is not a library, we will create a new "Group" for it and others of its kind. Choose "New Group" from the "Project" menu.

14-NewGroupRename.png (4929 bytes)

If you double-click the "New Group" heading, you can give it a more intelligent name. We'll call the new group "Code".

15-Organized.png (4560 bytes)

Ahh. That's better. (not that it makes any real difference, it just looks more organized)

Now, if you double-click on "App.cpp" in the Project window, you will be taken to a Code Editor window with that file open. This is good, since we will need to type in some code if we are to write and application.

16-ProjectCode.png (15949 bytes)

You can type whatever you like into this window, but it should be valid C or C++ code if you expect it to compile. You can save yourself the trouble of trying to read and re-type my dinky little program from the above image. You can download the source code right here. Unzip the contents, and then cut-and-paste or otherwise copy the code into your window. It is tested and functional. (although not very interesting)

Did you notice how I have the window tabs for my Code Editor window and my Project window lined up at the top? That makes it very easy to switch back and forth between them. If you want to get real fancy, you can move a window's tab to a different position by holding down the [Shift] key and dragging it with the mouse.

Running Your Application

Since we are creating a terminal-based application, it has to be run (or launched) from inside a Terminal. But before we can do that, we have to compile the code and link it with the necessary libraries. BeIDE makes this a piece of cake. Simply choose "Make" from any BeIDE "Project" menu.

17-Make.png (18526 bytes)

The compiler will start, followed by the linker. You can monitor the progress by watching the little blue check marks in the BeIDE Project window. If there are any errors, you'll get an Errors window with very detailed (yet cryptic) error messages. If you used my code, you should have no errors. When compiling and linking are finished, nothing will happen.

18-CodeSize.png (4612 bytes)

For each code file that has been compiled, BeIDE will display the amount of code and data (in bytes) in the compiled linker object. This gives you an idea of how bulky your code is, among other things. You can see here that my handiwork has been reduced to 286 bytes of machine language instruction and 40 bytes of data. Ouch.

You can see the freshly-created executable file in the project folder, in the Tracker.

19-AppFile.png (8656 bytes)

It should have an "Application" style icon. In this case, the application is named "BeSTL". Don't try double-clicking it, since it is a terminal-based application. If you do, it will run, but you will not see any output or be able to interact with it at all. Worse yet, if your program hangs or sits in a loop, it will tie up your CPU, with no obvious way of exiting the application. (If this happens, you can press [Ctrl]-[Alt]-[Del] and forcefully kill the application.)

For now, look, but don't touch. If you have installed OpenTracker, you should copy the project folder's path by clicking on it, and pressing the keyboard shortcut for "Copy". (which is [Ctrl]-[C] on my system)

20-AppPath.png (8728 bytes)

Now, open up a Terminal. This can be done by clicking on "Terminal" in the "Applications" section of the Be Menu.

21-TerminalMenu.png (17605 bytes)

You will land in a terminal window, sitting at a command prompt. "cd" (change directory) to the path of the project folder. If you didn't copy path this earlier on, you'll have to figure it out yourself.

22-TerminalPath.png (5272 bytes)

Now you might want to type "ls" (list) to verify that you're in the right directory, and display a list of files therein. Then simply type the name of your Application executable, and let the show begin.

23-TerminalAppRun.png (6734 bytes)

The output of the program is shown below.

24-TerminalAppOutput.png (9527 bytes)

Now, that wasn't so hard, was it?

Additional Information

At some point in time, you may want to open an existing BeIDE Project. This is very simple. Just find the BeIDE Project file that you would like to open (it should end with ".proj"), and double-click it.

25-OpenProject.png (8745 bytes)

This will launch BeIDE and open the selected project.

Now, "BeSTL" is probably not your first choice for an Application name. Neither is it mine. Fortunately, you can tell BeIDE what you'd like to name your finished, compiled, executable file. Choose "Project Settings..." from the "Edit" menu.

26-ProjectSettings.png (6660 bytes)

This will open the Project "Settings" window. If you choose the "x86 ELF Project" category on the left, you can type in a new name for your executable in the "File Name" box on the right.

27-SaveProjectSettings.png (9711 bytes)

Click "Save" and then close the Project Settings window.

One final note... You might be tempted to click "Run" on the BeIDE Project window's "Project" menu. Unfortunately, you are too young to click "Run" at this time. Doing so, for a terminal-based application, will have the same effect as trying to launch a terminal-based application from the Tracker. Namely, it will run, but you will not see any output or be able to interact with it at all. Worse yet, if your program hangs or sits in a loop, it will tie up your CPU, with no obvious way of exiting the application. (If this happens, you can press [Ctrl]-[Alt]-[Del] and forcefully kill the application.)

The "Run" command is, of course, useful for normal, non-terminal-based applications, which create things like windows and other snazzy user-interface objects. In our next tutorial, we will create a simple, graphical BeOS application, derived from the BApplication class and sporting a BWindow (and window thread) of its very own. But not until you're older.

Back to Top.