L01-rt Compiling a C program

In this lab, you will learn how to compile a C program in one of the UNIX environments available at Seneca.

C is an easy-to-read programming language(for programmers) that is not directly understood by a computer. A C program must be translated into machine language which is the language the computer was built to follow. A C compiler is a program that translates a C program that you have written into machine language. Once a C program is compiled (translated), the resulting machine language file can be run often without need for further translation. However, if you change the C program then you must recompile the program to update the machine code version.

You enter a C program into the computer using a text editor. You could use Notepad or WordPad or you could use text editors called nled, pico or vi on Matrix to create a text file containing your C program.Some of the nice free editors that you can load on your personal computer are TextPad and Notepad++. I happen to prefer these as they are coloured and have additional features.

The text file that you write and save is called the source file for the program. The source file is then translated by the compiler into a machine language program. That file is called the executable or binary file.

You will be using a Seneca computer called Matrix which is a Linux cluster running the SuSE Linux Operating system. This Linux operating system is similar to the UNIX operating system. This computer is able to be accessed by many students all at the same time.

I know this is a lot of new words for some of you. Have patience and you will get used to all the new stuff to learn.

STEP 1: -- 2 ways to connect to Matrix

Connect to matrix using a "secure shell" program.

(a) ON CAMPUS:

If you are using a computer on campus you should click on the SSH icon on the desktop.

(b) AT HOME: -- or on your personal computer if you don’t have SSH or equivalent

If you want to connect from your personal or home computer then download and install the SSH program by following the instructions. (Over time these instructions may need to be changed based on where they put the SSH software. Please let me know. Also Lab assistants can tell you where to locate its current location)

Got to here  you may need to log into the page more than once.

After you log on to the page you will see

From the menu select Course Schedule

On the right side select SSH/SFTP and install the software

IF … the above did not work, don't panic. Again, software over the years changes locations and names. There are many people(students) in the labs that can guide you to the latest locations of this software

Once you have the SSH loaded or if doing from Seneca labs

From either home or Seneca

Open the SSH folder and you will see the following screen

You then have to click on the Quick Connect button.

You will then need to connect to matrix by typing in the host name of matrix.senecac.on.ca

Enter your user ID  the my.senecacollege.ca user IDthat was given to you and click the Connect button.

When prompted for your password, enter your my.senecacollege.ca password.

Once you are logged in, you should see – your home computer or the one you are working with on left and matrix on right. You can drag files across from your computer to matrix

ASIDE: I prefer to use a product like Notepad++ to write my C programs at homeand save the file and then go to the open window above and drag it across. I then compile and run my programs on matrix. Any errors I correct in Notepad++ on my own computer and repeat the save, drag across, recompile and run process until it is perfect. I do this because I find the editors like Notepad++ to be better than the free editors on matrix.

The above is for moving files from one system to another. In the above example dragging from left side (my own computer) to the right side (my space on Matrix) will copy the file to Matrix.

To work interactively on matrix, meaning executing commands such as compiling your program, you need to open a second window/screen.

Select Window at the top navigation area and New Terminal in the drop down. You will get this screen to work with. Instead of my ID your ID will appear.

If you can access your Seneca email, but CANNOT access your Matrix account, then access the Academic Computing Systems (ACS) website and click on Create Matrix Account on the left hand menu (click on accept box at end of Policy Page and click on the Continue button, and then enter your My.Seneca learn account and password and click on the Create Account button) OR … ask the Lab assistants for help.

STEP 2:

To use the text editor on matrix you can use an editor called nled (neat little editor) to create a new file named first.c (C source file names must always end with .c) type in the following command:

nled first.c

A blank screen should appear.

 NOTE: You may prefer PICO or PINE as an editor instead on nled. These instructions are showing you nled.

STEP 3:

Type in the following C program exactly as shown:

#include <stdio.h

intmain( ) {

printf("This is my first program and my name is ?????? \n");

return 0;

}

You can make changes by using the arrow keys (also called cursor keys) to move around and the Backspace key to erase. (If the Backspace key does not work hold down the Ctrl (Control) key and then press either Backspace or the H key).

NOTE: One disadvantage to some of these simple editors are they are not GUI interface and so you can’t click anywhere in the code. You need to move by cursor keys to where you want.

STEP 4:(continuing with the use of the nled editor)

Save what you have typed by pressing the Esc (Escape) key, which will cause a "Command? " prompt to pop up. Then press the x key to save what you have typed and exit the text editor (x stands for ‘save and eXit’). A ~> symbol should appear

STEP 5:

Compile your program by entering the command:cc first.c

This may display some error messages, in which case you will need to go back to step 2 and correct what you had typed in.

If no errors are displayed, the compiler produces by default an executable file named a.out.

STEP 6:

When no error messages are displayed then run the program by typing in the command: a.out

Running the a.out file should display the following on the screen:

This is my first program and my name is

NOTE: Some programmers don't like to use the a.out name for the executable file, and prefer to give the executable file a unique name similar to the C source file. For example, you could compile your program using the command:cc first.c -o first and the executable file would now be called first, instead of a.out. The -o tells the cc command to name the "output" file whatever is typed in after the -o. You could then run the program by typing the command:first

STEP 7:

You are now going to change your program. To revise the file first.c type in the command:

nled first.c

Your program should then appear.

STEP 8:

Change the program so that it contains the following:

//This is my first C program –

//Written by: insert your name here

#include <stdio.h

intmain( ) {

printf("This is my first program\n");

printf("in IPC144\n\t using the C language\n\n");

return 0;

}

STEP 9:

Save what you have changed by pressing the Esc key and then press the x key. A ~> symbol should appear

STEP 10:

Compile your program using the command:cc first.c –o first (The -o tells the compiler to name the "output" file whatever is typed in after the -o.)

This may display some error messages, in which case you will need to go back to step 7 and correct what you had typed in.

If no error messages are displayed the compiler creates an executable file called first, instead of a.out. You can then run the program by typing the command:first

STEP 11:

Run the program by typing the command:first

Note how the output displayed on the screen has changed. Notice the effect of the \n and \t characters on the output?

STEP 12:SUBMISSION

To submit this lab program you will need to use a “script” program that will capture the screen output. When you exit the script file there will be a file named typescript.That is the file to transfer back to your own computer and submit to the instructor.

You are currently on matrix and your program runs successfully. You have tested it with different inputs.

Assume that the  is the prompt on your screen.

HOW TO SUBMIT:  very important as this will be used often

Write up the code, test that it is accurate. When it is working properly do the following on MATRIX.

At the prompt on matrix enter the following command

script< this script program takes over and captures your screen output

whoami< this identifies who is running the program

gcc first.c < assumes your program is called first.c

it demonstrates that first.c will compile

catfirst.c < this will list the program called first.c

a.outThis will run a compiled version of your program

exit < This will exit the script program and pass control back to the operating system

All of your output was put into a file called typescript.

You should have a look at what is going to be submitted by executing the following:

cat typescript

Download the file back to your own PC and rename it first.txt

The use of a .txt extension "may" make it easier for the instructor to open the email attachment.

Email the .txt file as an attachment.

The subject line states  L01 IPC144 section A

….. or D or E depending on your section of IPC144

Mail the result to me.

STEP 13:

To end you session on matrix, you MUST type in the command:

exit

and you will be "logged off". Your program will still be there the next time you log in. It is important that you end your session this way so that files are closed properly and so that your files will be secure.

You should then click on the File button and click on Disconnect from matrix.