Using Linux for CS31 Students

Getting started
Before attending lab on Friday afternoon, be sure that you can perform the following tasks. You are free to attend the cs31 ninja session from 7-9pm or 9pm-11pm on Tuesday September 2 to get assistance with these skills. Please consult the Using Unix Improved pages for help with some of these skills, including using an editor.
Compile and run a C program
Make a cs31/weeklylab/week00 directory and copy some files over to practice compiling a C program
[~]$ cd
[~]$ mkdir cs31
[~]$ cd cs31
[cs31]$ ls
[cs31]$ mkdir weeklylab
[cs31]$ ls
weeklylab/
[cs31]$ cd weeklylab/
[weeklylab]$ mkdir week00
[weeklylab]$ cd week00/
[week00]$ cp ~adanner/public/cs31/week00/* ./
[week00]$ ls
hello.c  Makefile  sample.c  sample_messy.c
[week00]$ pwd
/home/adas/cs31/weeklylab/week00
[week00]$ 
Source files with a .c extension must be compiled before being run. We will use the compiler gcc to compile our programs.
[week00]$ gcc hello.c -o hello
[week00]$ ls
hello*  hello.c  Makefile  sample.c  sample_messy.c
[week00]$ ./hello 
Hello world
[week00]$
The -o option allows us to specify the name of the compiled executable. In this case, the name is hello without the .c extension. If the program compiles successfully, you can run the program by typing ./hello

In some instances, we will provide a Makefile to help automate the compile steps. If you type make at the command prompt, it will automatically build any necessary files needed as specified in the Makefile. Don't worry too much about Makefile syntax at this point. If you make changes to any of the source code, you will need to recompile or run make to compile new executables. Since the executables are automatically built from the source, it is safe to remove them. This is usually done using make clean.

Try reading, editing, compiling, and running the three sample programs provided.

More to explore
For a bit more detail on compiling C programs on our system, see Prof. Newhall's gcc basics. For a quick summary of the C programming language please read C for Python programmers through section 2.5 before lab on Friday. We will talk more about the C programming language in week two of the course and throughout the semester. For now, understanding the basic programs provided will be sufficient for the first lab.