Lab 0: Getting Set Up!
This lab guides you through setting up the development environment. Nothing need be submitted for this lab; it is not for marks!
Prerequisite Skills for This Lab
- None!
1. Creating Dev Environment
This course is opinionated about the development environment that students must use to complete labs and assignments. You must use the command line for all compiling and running of your program, and you must use NeoVim to write your code. All of this must be done inside a pre-configured Docker container to provide you all the necessary tools. Assignments A0 and A1 will teach you how to use the command line and how to use NeoVim!.
Docker is a program that allows you to run light-weight virtual machines called containers. Each container is isolated from the rest of the system: your programs can only interact with the files and programs inside your container. Each container has its own programs as well.
Technically, a docker container is what you get when you first download and then run a docker image.
Right now, go read and follow the Resources page of the course website. Do the steps yourself as you read the directions for "Docker Container Setup". This will guide you through installing docker, downloading the CMPT201 image, and running it as a container.
2. Launch and Set Up Git Repo
- Close any open terminals from the above steps.
- Open your development terminals:
- I suggest opening 3 tabs / terminals: It's faster to switch tabs that close/open NeoVim.
- Tab 1: Use for
man
pages. - Tab 2: Use for editing code in NeoVim.
- Tab 3: Use it to compile and run program.
- Tab 1: Use for
- See Resources page for details on the Docker commands.
-
Hint: Docker command summary
You'll need to know (memorize?) the following commands:
- First, make sure Docker is running by launching the Docker Desktop program on your computer. Once launched, you can close it immediately if you like because just loading it once starts the Docker Engine, which is all we need.
- Each time you work on CMPT 201, you'll need to start the container once inside one of your terminal tabs:
docker start -ai cmpt201
- Then, in each additional terminal tab, run this command to connect to that same docker container as above:
docker exec -it cmpt201 zsh
- I suggest opening 3 tabs / terminals: It's faster to switch tabs that close/open NeoVim.
- Generate an SSH key:
- In one of your Docker container terminals, check if you already have an SSH key:
cat ~/.ssh/id_ed25519.pub
- If it says "'No such file or directory'" then:
- Create one (changing "your_email"!):
ssh-keygen -t ed25519 -C "your_email@sfu.ca"
- When prompted for a file, passphrase, or other details, just press ENTER.
- Now repeat the above
cat
command to display your public SSH key for this container.
- Create one (changing "your_email"!):
- In one of your Docker container terminals, check if you already have an SSH key:
- Configure GitHub to trust your Docker container:
- Go to GitHub.com and login. Create an account if you don't have one.
- In the top right, click your profile photo.
- Go to Settings > SSH Keys
- Click "New SSH key". Name it something like "CMPT201 Container Home", and then copy-and-paste the output of the command:
cat ~/.ssh/id_ed25519.pub
- Click "Add SSH key".
- If you want more detailed directions, you can follow this GitHub guide for setting up an SSH key.
- Create a personal GitHub repo for your work in the course.
- On GitHub.com, create a new repository ("repo") for your personal work and labs:
- Click "New" on the repository list (or just make a new repo):
- Repository Template: None
- Owner: Your GitHub account name (likely already filled in for you)
- Repository name: Recommended using "cmpt201-coursework" or the like
- Add a README file: Yes
- Add .gitignore: C
- Click "Create Repository"
- Click "New" on the repository list (or just make a new repo):
- On your new repo's page, click the green "<> Code" button
- On Local tab, select "SSH", then copy the string.
- String should be like "
git@github.com:<user_name>/cmpt201-coursework.git
"
- String should be like "
- On GitHub.com, create a new repository ("repo") for your personal work and labs:
- Clone the repo into your container:
- In your terminal, change to the home directory:
cd ~
- Clone your repo (pasting in the last part of the string because you just copied it!)
git clone git@github.com:<user_name>/cmpt201-coursework.git
- Congratulations! You now have a
~/cmpt201-coursework folder
that is connected a GitHub repo! Put your lab and personal code in this folder. - Don't use this repo for assignments: each assignment will have its own dedicated repo.
- In your terminal, change to the home directory:
- Complete the CourSys "Quiz" that asks your GitHub user name.
- Do this on CourSys.
- We need this to mark your assignments because they are all on GitHub only.
3. Create C Program
- Create a folder for your lab:
- Change to your repo:
cd ~/cmpt210-coursework
- Create the folder:
mkdir lab0
- Enter the folder:
cd lab0
- Change to your repo:
- Create a simple C program that prints "Hello CMPT201 world!" to the screen.
- Launch NeoVim:
nvim lab0.c
- Enter "Insert" mode to start typing code by pressing
i
. - Type your code to print a message or two to the screen:
-
Hint: C Code
Type in (don't copy and paste!) the following code:
#include
int main() { printf("Hello CMPT 201 World!\n"); printf("This is gonna be a wild ride!\n"); }
-
- Exit insert mode: ESCAPE key
- Save file by typing "':w'" and press enter.
- Launch NeoVim:
- Compile and run in another tab
- In another tab, change to your repo's directory.
- Compile with the address sanitizer so it displays errors on bad pointers when running:
clang lab0.c -fsanitize=address
- Run with
./a.out
-
Hint - Compile and Run
Combine compiling and running into one command line command using && (AND):
clang lab0.c -fsanitize=address && ./a.out
- Push your changes
- Add files to Git:
git add lab0.c
- Commit changes:
git commit -m 'Lab 0 work'
- Push changes:
git push
- Add files to Git:
- Close NeoVim:
- Exit insert mode, if needed: ESCAPE key.
- Save and close by typing: "':wq'"
Submission
Normally, labs will be submitted to CourSys and marked for completion; however, this lab is not for marks and nothing need be submitted.
If you were working on a CSIL machine, delete your docker container: docker rm cmpt201
(Don't do this if you were on your own laptop!)