Lab 1
Editing, Compiling, and Executing in the Linux environment; Using turnin

Objectives

Note: don't worry if you can't finish the entire lab exercise. Use turnin (see the last step) to turn in as much as you've completed before you leave the lab. Make sure you finish the rest of the lab on your own time.

What you should do...

  1. Sign the attendance sheet.

Working with Linux

  1. If XWin32 is running on your computer, there will be a blue-X/monitor icon in the system tray at the lower right hand corner of your screen. If XWin32 is not running you must start it on your computer:
click Start --> All Programs --> Utilities --> XWin32 8.0 --> XWin32

XWin32 is a terminal application for Windows computers, which allows Windows users to connect to Linux servers on a local network or via the Internet. X applications running on those servers will be displayed onto the Windows desktop. There are two reasons why we run XWin32 when connecting to Linux:

  1. Log onto the Linux system using your CCC username and password:
  2. Create a directory called cs2301 and make it your working directory. You can use the following Linux shell commands:– mkdir cs2301 (make directory) and cd cs2301 (change directory). A directory in Linux (or Unix or most real-time systems) is like a folder in Windows or the Macintosh; you can organize your files by storing them in different directories. Try the command pwd (print working directory). Now go back to your "home" directory by typing cd .. (the ".." means "move up one level in the directory structure"). Type pwd again. Finally, make cs2301 your working directory again by typing cd cs2301 one more time.
  3. Use emacs to create a C source file called lab1.c. The command to start emacs in its own window is emacs lab1.c& (it is the & that signifies that emacs is to be run "in the background"). After you enter this command, you will have two windows open; we'll call them the Linux shell window and the emacs window.

Note that C source files cannot be created with word-processing applications (like Microsoft Word) that embed formatting information in the files. You must use an editor like emacs (or pico) that produces straight ASCII text files.

  1. Click in the emacs window and enter the following program (you could copy/paste, but typing it in will get you used to C syntax):–
/* Lab 1
   Getting started
 */
 
#include <stdio.h>
 
int main()
{
  /* Print a single string on "standard output" */
  printf("Hello, World!\n");
  
  return 0;
}

Be sure that there is a newline character (i.e., a carriage return) after the last curly bracket. Save the file. [Note: this program is very similar to the program in §1.1 of the Kernighan and Ritchie textbook.

  1. In the Linux shell, compile the program with the command gcc -Wall lab1.c (the -Wall instructs the gcc compiler to display all warning messages). If the compiler reports any error or warning messages, make sure your file looks exactly like the program given above. If you find any discrepancies, make changes and recompile. When you can compile with no errors, run the program with the Linux command

./a.out

(This is a strange-looking command. When Linux creates an executable file it names it a.out. The ./ preceeding a.out tells Linux to look for the a.out file in the working directory (the "." means "the working directory")). (Ask a classmate or a TA if you get stuck.)

Compilation Errors

  1. Click in the emacs window. Edit the lab1.c file by deleting the semicolon character at the end of the line starting with printf. Save the file, then compile it. You should see a compilation error message that looks something like this:–
lab1.c:  In function 'main':
lab1.c:12: error: parse error before "return"

The 12 refers to line 12 in your source file (if you go to the emacs window and click on the first printf statement in the program, you will see L15 displayed at the bottom of the window, telling you the cursor is on line 12). Line 12 is actually syntactically correct. The error occurs a couple of lines above, but it wasn't until the compiler started working on line 12 that it detected a problem. This happens frequently. If the compiler tells you it found an error on a particular line, but you are sure that there are no errors on that line, then search for the error on the lines above the flagged line (start with the line immediately preceeding the flagged line and work backwards). Put the semicolon back where it belongs and recompile.

  1. Introduce another error by deleting the 0 from return 0; This time, compile with the command gcc lab1.c. No compilation errors are displayed. Compile again, this time with Warning messages turned on (gcc -Wall lab1.c). Now you'll see a warning. The error you saw in step 10 was a fatal compilation error — that is, the compiler couldn't translate your program into executable code. Warning messages, on the other hand, are not fatal — an executable program is created and you can run your program. A warning is displayed when some part of your code is syntactically correct, but contains a potential error in logic. Warning messages should always be taken seriously. If you get a warning message, figure out why and make changes. Fix the error by putting the 0 back in, and recompile.
  2. This time, replace the line

printf("Hello, World!\n");

with the following lines:–

printf("Hello, ");
printf("world!");
printf("\n");

Compile and run your program again. See what happens. Also try deleting the ‘\n’ from the original program and see what happens.

  1. On the third line, remove the * before the /. Compile. Whew! Sometimes a minor syntax error can generate an awful lot of compilation errors. If you get a screenful of compilation errors, start at the beginning of the list and try to fix the first one reported. Often, fixing one error results in many subsequent "errors" being fixed. Put the * back in and recompile.
  2. On the line beginning with #include, insert two ‘/’ characters at the beginning of the line, so that it reads

//#include <stdio.h>

Compile your program again. See what happens this time. Can you explain this?

  1. If you have time left, introduce new errors on your own (for example, take out a quotation mark, misspell a name, leave out a parenthesis, etc.). The more familiar you become with compilation error messages now, the easier time you'll have debugging programs in the future.
  2. Turn in your files using the Linux turnin program (click here if you are unfamiliar with the Linux version of turnin). The Linux command you should use to submit your files is

/cs/bin/turnin submit cs2301 lab1 lab1.c

See you next week!