Bash shell script for compiling C code

First find a directory that you have write permissions in that is also in your executable search path. Usually in a Linux system a subdirectory 'bin' under your home directory is in your default search path. You can check this by issuing the command

        echo $PATH

to display the value of the shell variable 'PATH' and inspecting the colon-separated list of directories. If your 'bin' subdirectory does not exist, you may create it.

Note that some Linux systems are set up to only include ~/bin in your executable search path if that directory already exists when you log in. This includes the Ubuntu Linux systems in our class lab. If the PATH variable does not include /home/user/bin when you display it above, then you must create the bin directory, then log out, and then log back in as 'user' to set up your path correctly. To illustrate in the case where there is no ~/bin directory yet:

        $ cd
        $ ls bin
        ls: cannot access bin: No such file or directory
        $ echo $PATH
        $ mkdir bin

Now log out and log back in as 'user'. (You don't have to reboot the system, only log out and log back in.) Now check the search path:

        $ echo $PATH

Note that ~/bin has now successfully been included at the beginning of the search path. Then download this file: c3340 and save it into that directory in your search path. Change into that directory and issue the command

        chmod u+x c3340

This will set the the file access permission bits to include execution as a bash shell script.

From any directory that has your class C source code files, you may now use the new command 'c3340' as a convenient shortcut for compiling C code in the manner we are using in the class. The script will accept any number of C source code files as command line arguments, and first does some checking to make sure all the named files exist. Then all the source code files will be compiled and linked together to form the executable binary file by invoking the gcc command as we have been doing in class. The executable file will be given the name of the first source code file with the '.c' suffix stripped off. The '-Wall' warning level and '-lm' math library search arguments are also automatically added to the gcc command line.

So for example, if you were to issue the command

        c3340 file1.c file2.c file3.c

This shell script will assemble the gcc command line

        gcc -Wall -o file1 file1.c file2.c file3.c -lm

display the assembled command on the terminal, and then execute it in the current working directory.