Writing and Compiling programs in Linux

Section 1: Brief Outline

A: Log into your account.

B: Launch NEdit by clicking the NEdit icon on the application panel.

C: Type your program code.

D: Save your program by selecting Save As from NEdit's File menu.

E: Open a terminal window by single-clicking the 'Terminal' icon on the task bar along the bottom

F: Compile your program by typing:

    g++ -Wall programName.cpp -o programName

or, to run the debugging:

    g++ -g -Wall programName.cpp -o programName

G: Run your program by typing 'programName' in the terminal window.

H: Print your work by selecting the text you want to print and choosing Print from the menu.

I: Log out by selecting Log Out from the footprint menu.

J: Frequently Asked Questions.

Section 2: Detailed Instructions

A: Logging In.

Begin by finding a free computer in CMC 306. If no one else is logged in, the computer will be displaying the Linux login panel. First, make sure your pointer is somewhere on the login panel. Then, type your username, then password in the spaces provided.

If you do not have a Linux account and would like one, see Mike Tie in CMC 305.

B: Launching NEdit.

NEdit is the name of the simple text editor for linux. You will use it to enter and save your program code. To run NEdit, simply click on NEdit icon on the application panel. This will open up a NEdit window. If you are reading this document for the first time, you should enter the text below in the NEdit window.

Be sure your text is an exact replication of what is shown below. If you misspell anything or miss any punctuation, your program will not compile.

Hello World!

In case the above image is blurry, use the following text:

#include <iostream.h>

int main() {

cout << "Hello world!" << endl;

}

C: Type away.

Save often, every five minutes is good. See below.

D: Saving your work.

After you have typed a few lines, you should save your program to avoid data loss. Also, save your program periodically while you are working on it, and remember, you must save your program before you compile it. To do this, choose save as from the file menu. The first time you do this, you will see a panel like this:

Save dialogue for NEdit

Click on this panel. To save your document, first give it a name, by typing in the Selection field, then click on the OK button or press the enter key on the keyboard. This will create a file in the current directory with the name you specified above. In the example above, the name "myprogram.cpp" has been entered. A NOTE ABOUT NAMING: you should end the names of your programs with ".cpp" - this is called an extension, and the computer recognizes ".cpp" as a C++ source file.

NOTE: You need to remember which directory (or folder) you saved your program in. Unless you have created a new directory, this will be in your home (username) folder.

At this point, you may continue typing in new code or editing the code you have already typed. Remember to save your work periodically by choosing Save from the File menu. If you have already saved a document, you will not see the save window again. Your older saved version will be updated with the more recent version, and you will not be asked for a new file name. If you do not wish to update the old version, and you want to name the current file something different, choose Save As instead of Save from the File menu.

E: Opening a Terminal Window.

Now that you have completed writing and saving your program, you are ready to compile it.

To compile a program with g++, you need to access a "terminal window," a window in which you can tyoe commands at what is called a "prompt." This is essentially a way to interact with a computer using only words, i.e. no mouse, no graphics, etc.

To open a terminal window, just click on the Terminal button on the task bar along the bottom of the screen. This will bring up a terminal window on your screen. In this window you will type the commands necessary to compile and run your programs, and you will see the error messages, warnings, and other information given to you by the compiler. Let's suppose you've entered a program in a NEdit window and saved it already under the name "myprogram.cpp".

We will use the 'hello world' (above, section B) for illustration.

F - G: Compiling and Running Your Program.

To compile this program, you first need to make sure you are in the correct directory. If you saved your file in a directory other than your home directory (see the note in section D), then at the command prompt in a terminal window type:

    cd DirectoryName

To check that your file is in this directory, type the command 'dir' in at the command prompt. You should see a list of file names in a column near the right of the window. Once you are in the correct directory, type the following command at the prompt to compile your program:

Compiling a program

A brief explanation of this command:

  • "g++" is the name of the compiler program.
  • -o" tells g++ that the next string will be the name of the executable. Normally g++ creates a file named "a.out" from the information in the source code, but this command tells it to name the file "myprogram".
  • "myprogram" is the name given to the executable.
  • "myprogram.cpp" is the name of the file you are sending to the compiler.

The compiler then creates an "executable" file called 'myprogram'. When you run this, it will cause the words 'Hello World' to be printed on the screen, followed by a carriage return. You can run this program by simply typing myprogram at the command prompt. The output of the program will appear in the terminal window, as shown below:

Running the program

And that's all there is to writing, saving, compiling and running a C++ program. These same basic steps can be used to enter and compile any C++ program. If your program does not compile or run correctly, it is easy to fine tune it. You can keep your source file open in a NEdit window and make small changes after you run it. Then save and re-compile it to run the new version. Remember , you must save your program before you compile it to have any change take effect.

H: Printing your work.

Any time you want to take your program home with you to figure out why it isn't working or hand in a copy to your professor, you will want to print the source code (that is, the ".cpp" file). To print your program code return to the editor by clicking on the editor window. If you cannot see the window, click on the appropriate button on the right side of the task bar, and then click on the window. Choose Print from the File menu. A panel for printing will appear. Click Print, and your code will be printed on the printer in the room you are working in.

Printing dialogue box

I: Logging Out.

It is very important to log out of the computer when you are done with a programming session. If you do not log out before you leave your machine, someone else could come by and accidentally (or maliciously) delete some of your files or otherwise abuse the system. To log out, simply click on the footprint and choose the Log out option from the menu. A new alert panel will appear in the middle of the screen asking you if you really want to log out. Press the Enter key or click on the Yes button to complete the log out process. Make sure you have successfully logged out by waiting for the login panel to reappear before leaving your machine.'

J: Frequently Asked Questions (FAQ):

Where can I get extra help?

There are Lab Assistants in CMC 306 every night from 7pm to 11pm and some afternoon hours. A lab assistant schedule is posted on the web and in the labs. If there isn't a lab assistant available and you have a technical question, then ask Mike Tie for help. Mike is the Math/CS technical assistant, and his office is in 305, next door to the Intro CS lab. If there isn't a lab assistant available and you have a programming question, see your instructor.

My program is hung! What do I do?

You should be able to stop any program (either hung or operating normally) by simply pressing the 'Control' key (lower left of the keyboard labeled 'Ctrl') and the 'c' key at the same time. This will return you to the command prompt of your terminal window.