Many of you are used to environments similar to Eclipse for code development (editing, compilation, and execution). In cmsc216 we we will perform the same tasks using the tools provided by the Linux operating system. Linux is a variation of Unix.
Although you could install linux in your computer, we will rely on a computer system the university provides (grace.umd.edu). Anyone registered in this course will have an account on this system. The account id will be your directory id (e.g., terps) and the required password, your usual password. To access grace.umd.edu you need internet access and a program (ssh client) that will establish a connection between your computer and grace.umd.edu. Establishing a connection will create a window on your computer where you can execute commands.
Using an ssh client (see Logging on / Logging off to grace.umd.edu) log on to grace. When you log on you will land in the home directory (a directory is another name for folder). In this directory you can create files and directories. A prompt (e.g., grace2:~:) will appear after which you can execute commands. You execute commands by typing the command and pressing enter. To log out you can execute the logout command.
Each time you log on to Grace you need to authenticate using Duo (e.g., your phone). By using a VPN connection (see UMD VPN) you can prevent multiple authentication requests. First, access the school VPN and then log on to Grace as described above.
Linux has tons of commands, but we want to focus on the ones that will allow us to write and execute C programs.
You can create files by using editors (e.g., emacs, vim) or other Unix commands. Initially we will use a simple text editor named nano, so we can focus on creating files.
To create a file using nano, type nano followed by a file name (e.g., nano p1.c). If the file already exists, the file will be opened, otherwise it will be created. Once you open a file, you will see at the bottom a commands summary. The ^ represents the CTRL key. Once you have opened the file, you can just start typing and use the arrow keys to move around. To save and exit execute ^X. At this point try creating a file with the following contents:
#include <stdio.h> int main() { printf("I like Pepsi\n"); return 0; }
To compile a C program, you will use the gcc command. To compile the program p1.c you created earlier, execute the command gcc p1.c. If there are errors, you will see a listing of those errors; otherwise you will get the prompt back. If there are no errors, the compiler would have created a file called a.out. If you type a.out at the unix prompt, the program will be executed. The file a.out is overwritten each time you compile. It represents the machine language equivalent of the C program. Unlike Java programs where you compile to bytecode, and that bytecode can be executed by any java virtual machine, C code compiled for an environment will not necessarily work on others (you need to compile again). At this point you should compile p1.c and introduce errors to see the messages generated by the compiler.
Several gcc compiler options are available that can provide additional warnings, generate code with different levels of effciency, etc. For example, gcc -Wall p1.c will provide additional warnings during compilation. For cmsc216 we have defined the options you need. To avoid having to type all those options each time you compile, you can create an alias for gcc. After you setup your account (see below) try setting the gcc alias. Information on how to set the alias can be found at setting gcc alias.
There is command (e.g., setup_216) to configure your account so you can complete projects. This command will configure your account by:
If unfortunately you are repeating this course, you need to make sure you reset your Grace environment before you run the setup command. Please follow the instructions your previous instructor provided regarding resetting your Grace environment. Some resetting information is available at https://www.cs.umd.edu/~nelson/classes/resources/resetting_grace_env.html.
To run the setup command for cmsc216 fall 2024, copy and paste the following command at the linux prompt, log out, and log on again. For other semesters, contact your instructor for the appropriate information. Note: even though the command specifies section 0401, the command is valid for all 04XX sections (just cut and paste what we have provided).
/afs/glue/class/fall2024/cmsc/216/0401/public/bin/setup_216
The following changes would have taken place after executing the setup216 command:
cp -r ~/216public/lecture_examples/Week01 ~/216
Make sure you run the setup216 command and you set the gcc alias as described above. That is all the setup you need for the course. You will execute the setup216 command and set the alias only once.
Start exploring linux commands by practicing the commands below. Make sure you are in your 216 directory.
Once the semester is over, make sure you reset your Grace account/environment as described at Resetting Grace Environment
set prompt="[%m %~: ] "%m stands for the machine (e.g., grace1), %~ represents the current directory. Here is how the prompt will look like if we were in the tmp directory. Other options to try: %M, %n.
[grace4 ~/tmp: ]