CMSC 123 Tutorial

Summer 2007-2008

This tutorial will introduce you to the process of creating and submitting laboratory exercises in CMSC 123. In every exercise, you will be handed a tar file that contains a set of files that will serve as your starting point. Let's get started..

  1. Download the sample tar file: 123sample.tar.gz
  2. Extract the tar file:
    $tar xzvf 123sample.tar.gz
  3. You'll get following files inside the sample folder: Makefile, max.h, max_test.c,main.c,README

  4. Go inside the sample folder:
    $cd sample
  5. Run make:
    $make
  6. make: *** No rule to make target `max.c', needed by `max.o'.  Stop.

    The error above will be displayed, which simply means that max.c is needed to build the program but it was not found.

  7. Create an empty file max.c then run make again:
    $touch max.c; make
  8. gcc  -c max.c
    gcc  -c max_test.c max.o -lcunit
    gcc: max.o: linker input file unused because linking not done
    gcc: -lcunit: linker input file unused because linking not done
    gcc  -o max_test.exe max_test.o max.o -lcunit
    max_test.o(.text+0x24): In function `test_maxi':
    : undefined reference to `maxi'
    max_test.o(.text+0x5d): In function `test_maxi':
    : undefined reference to `maxi'
    max_test.o(.text+0x95): In function `test_maxi':
    : undefined reference to `maxi'
    collect2: ld returned 1 exit status
    make: *** [max_test.exe] Error 1
    

    The above error will be displayed which indicates that there is an undefined reference to a function maxi which is true because we have not defined a function named maxi yet.

  9. In order to define maxi, let's look at the prototype of ths function in the header file max.h
  10. #include 
    #ifndef __MAX_H__
    #define __MAX_H__
    
    int maxi(int i1, int i2);
    
    #endif
  11. We now define the maxi function based on the prototype inside max.c
  12. #include <stdio.h>
    #include "max.h"
    
    /**
     * Returns the maximum of two numbers
     *
     * @param i1 the first number
     * @param i2 the second number
     * @return the maximum of i1 and i2
     *
     */
    int maxi(int i1, int i2){
       int retval=0;
    	if (i1 > i2){
    		retval = i1;
       }else{
       	retval = i2;
       }
       return retval;
    }
    
  13. Run make again:
    $make
  14.      CUnit - A Unit testing framework for C - Version 2.1-0
         http://cunit.sourceforge.net/
    
    
    Suite: Suite_1
      Test: Test of maxi() ... passed
    
    --Run Summary: Type      Total     Ran  Passed  Failed
                   suites        1       1     n/a       0
                   tests         1       1       1       0
                   asserts       3       3       3       0
    
    

    The result above shows that all the test passed which means that we have properly defined the function maxi. If you want to see how maxi was tested, look inside max_test.c. CUnit is used as the unit testing framework.

  15. Document your work by editing README
  16. Indicate your initials in Makefile. You can use your initials.
  17. INITIALS=jach
  18. To submit the finished exercise:
    $make clean;make submit
  19. A file named jach-sample.tar.gz will be created in the directory above sample. This tar file is what you are going to submit to your instructor.

  20. Summary
  21. To do your exercises in the class, the following steps will be followed.

    1. Download the exercise tar file.
    2. Define the functions declared in the header file.
    3. Make sure that the functions passed the defined tests.
    4. The tests are defined by the instructor.
    5. Your grade for the exercise is equal to (passed asserts/total asserts)
    6. Document your work in README
    7. Submit your work.


$Id: 123Tutorial.html 474 2008-04-22 01:35:48Z jachermocilla $