Tips


Tutorials

Header Block


Good C programs typically have a descriptive header block similar to this:

Header Block

/***************************************************************************/
/*                                                                         */
/*  NAME:       this-file-name.c                                           */
/*  VERSION:    1.0                                                        */
/*  AUTHOR:     Your Name <you@youremail.com>                              */
/*  CREATED:    Timestamp (output of 'date -u')                            */
/*  MODIFIED:   Timestamp (output of 'date -u')                            */
/*  COURSE:     ET2560                                                     */
/*  ASSIGNMENT: Assignment Number                                          */
/*  INSTRUCTOR: William Stone III                                          */
/*                                                                         */
/*  PURPOSE:    This program does some work.  This section should include  */
/*              a short description.                                       */
/*                                                                         */
/*  PSEUDOCODE: This section should include the pseudocode of the program. */
/*                                                                         */
/***************************************************************************/

When the compiler sees this block, it will ignore it.  This is very useful documentation of the kind found inside virtually all source code files.

Compiling Under UNIX/Linux


Source code for this course should compile correctly in the UNIX or Linux command-line environment.

In order for this to be successful, the GNU C Compiler (GCC) must be installed.  If it is not, follow your distribution's instructions for installation.

If you wish to compile to code in UNIX/Linux, execute the following command:

    gcc -Wall -g $SOURCE -o $TARGET

where $SOURCE is the name of the C source code file and $TARGET is the name you wish the program to be called.

Compiling Under UNIX/Linux With a Script


Source code for this course should compile correctly in the UNIX or Linux command-line environment.

In order for this to be successful, the GNU C Compiler (GCC) must be installed.  If it is not, follow your distribution's instructions for installation.

You may create a script in your path to help compile C programs more easily.

Create a script called nomake that reads:

nomake

#!/bin/bash
#############################################################################
#                                                                           #
#  NAME:     nomake                                                         #
#  VERSION:  1.0                                                            #
#  AUTHOR:   William Stone III <wrs@wrstone.com>                            #
#  WRITTEN:  Thu Sep 13 15:00:43 UTC 2012                                   #
#  MODIFIED: Thu Sep 13 15:00:43 UTC 2012                                   #
#                                                                           #
#  PURPOSE:  This script compiles simple C programs for which no Makefile   #
#            or similar applications exist or are configured.               #
#                                                                           #
#            The program calls GCC to compile the specified C source code   #
#            file.  It strips off the ".c" extension from the source code   #
#            filename and uses this as the target binary name.              #
#                                                                           #
#############################################################################

# Get the current program name
PROGRAM=`basename $0`

# Check to see if the user entered a C source code file
if [ "$1" == "" ]; then
        # The user didn't specify a source code file, so let's give them instructions
        echo
        echo "Usage:  $PROGRAM [C Source Code File]"
        echo "Where [C Source Code File] is the name of a C source code file."
        echo
else
        # The user called the script correctly, so we can do work

        # Set the source to the C source code file provided
        SOURCE=$1
        # Set the target to the same name as the source code file without an extension
        TARGET=`basename $1 .c`

        # Call the GCC Compiler
        gcc -Wall -g $SOURCE -o $TARGET
fi

Example

To use this script, simply type:

    nomake $SOURCE

where $SOURCE is the C source code file.

To compile a program named lab-3.1.c, one would issue the command:

    nomake lab-3.1.c

This will produce a program called  lab-3.1  that can be executed with the command:

    ./lab-3.1