---
# System prepended metadata

title: recitation 1

---

# Recitation 1

## Topics today
* [Access the iLab](#Access-the-iLab)
* [Create and edit a C source code file](#Create-and-edit-a-C-source-code-file)
* [Compile and execute a program](#Compile-and-execute-a-program)
* [Basic Linux Commands](#Basic-Linux-Commands)

## Access the iLab
All the information needed to access the iLab is included in this link [iLab 101](https://resources.cs.rutgers.edu/docs)
But still, I want to walk you guys through the whole process:
### Set up your iLab account
  [Activate](https://services.cs.rutgers.edu/accounts/activate/activate) your iLab Account and [Setup you CS password](https://services.cs.rutgers.edu/accounts/changepass/changepass).(Note that this password is different from your University password)
### Multiple ways to get control of an remote machine: 
#### Web login: 
To use WebLogin, go to [weblogin.cs.rutgers.edu](weblogin.cs.rutgers.edu) and enter your Rutgers NetId and CS password in the login screen below.
#### Secure Shell(SSH) Login (Recommended)
1. Install SSH on your computer
* [Windows](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse?tabs=gui)
* MacOS should have pre-installed OpenSSH for you. If not, please just run the following command in the Terminal.app:
    ```bash=
    brew install openssh
    ```
    If you haven't installed brew... Please refer to [Homebrew](https://brew.sh/) and check it out! You will definitely need it as a CS majored student.
* Ubuntu 
    ```bash=
   sudo apt install openssh-client 
    ```
2. After installing SSH, you can try to use it from your terminal:
    ```bash=
    # Format: ssh <netid>@<name-of-ilab-machine.cs.rutgers.edu>
    # Example:
    ssh dz323@cpp.cs.rutgers.edu
    ```
    You will be required to type in your CS password. And then, Enjoy!
    As for the `<name-of-ilab-machine>`, feel free to choose one you love the most from the [list](https://report.cs.rutgers.edu/nagiosnotes/iLab-machines.html).
3. Set up your SSH configuration and SSH keys to make life eaiser (**Optional**):
   Here's a more comprehensive tutorial if you want to know more about it [SSH Keys](https://hpc-wiki.info/hpc/Ssh_keys)
    * Generate your ssh key:
        ```bash=
        ssh-keygen
        ```
        You will be prompted to set up the name of the key, path to the key and the passphrase for the key. Feel free to just hit `ENTER` and everything will be setup by default values. After the setup, if you didn't input the name and the location of the key, it should be saved at `~/.ssh` with the name of `id_rsa.pub`(public key) and `id_rsa`(private key). Please **DO NOT** give your private keys to anyone just like you don't casually give out your bank account passwords because it can cause serious **Security Issues**.
    * Copy your public key to the remote server(Here the remote server means any computer from the iLab list)
        ```bash=
        # Format: ssh-copy-id -i /path/to/your/public/key <netID>@<name-of-ilab-machine.cs.rutgers.edu>
        # Example: 
        ssh-copy-id -i ~/.ssh/id_rsa.pub dz323@cpp.cs.rutgers.edu
        ```
     * Test your key(In this way, you will not be required to type in your password every time)
         ```bash=
        # Format: ssh - /path/to/your/private/key <netID>@<name-of-ilab-machine.cs.rutgers.edu>
        # Example: 
        ssh -i ~/.ssh/id_rsa dz323@cpp.cs.rutgers.edu
         ```
     * Save your configuration
         If everything works so far, you can choose to save the configuration to ~/.ssh/config:
         ```=
         #Format:
         #Host <any nickname you want to use for this remote server>
         #    HostName <the domain or ip address of your remote server>
         #    User <your netID>
         #    IdentitiyFile <you private key file>
         Host cpp
            HostName cpp.cs.rutgers.edu
            User dz323  
            IdentityFile ~/.ssh/id_rsa
         ```
       And then you can just do
       ```bash=
       ssh cpp
       ```
       To connect to your remote server.
       **Update:** iLab disabled SSH Key authentication because of some security issues. Please just use your password.
#### Visual Stuio Code(Optional)
If you are a big fan of VSCode, you can actually use VSCode for everthing. Just install VSCode, intall its SSH extension and configure your ssh settings in VSCode(Actually, if you have already set everything up in your `~/.ssh/config`), and then enjoy coding on VSCode! Here are the link to the [tutorial](https://code.visualstudio.com/docs/remote/ssh).

### Transfer your files to the server
* **Github**
    Create a repo under your github account, and then clone it at the server. Git is preinstalled at all the servers at iLab.
* **SCP(Secure Copy)**
    `scp` uses the same authentiacation as SSH. Thus once you have configured your SSH, you can use scp to transfer files.
    ```bash=
    # Format: scp [file_name]  remoteuser@remotehost:/remote/path
    # Example:
    scp ~/test.c dz323@cpp.cs.rutgers.edu:~/Documents/
    ```
    Note that if you are transfering a whole folder, please add the `-r` flag after scp:
    ```bash=
    scp -r ~/test/ dz323@cpp.cs.rutgers.edu:~/Documents/
    ```
    Also, Note that this `scp` is different from the [SCP Foundation](https://scp-wiki.wikidot.com/).
## Create and edit a C source code file

### Creating a New C Source Code File
In the terminal, use a text editor of your choice to create a new C source code file. You can use popular Linux text editors like Vim or Nano. For example:
```bash=
vim my_program.c
```
     
     
Inside the text editor, you'll see an empty file. Once Vim has started, you need to enter Insert Mode so that you can start writing code. Press i to enter insert mode.

### Writing C Code
In the text editor, write a simple C program, such as the "Hello, World!" example:
```c=
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
```
### Save File
* After entering the code, press Esc to exit insert mode and return to normal mode.
* Type `:w` to save the file.
* To exit Vim, type `:q` and press Enter. If there's something wrong and you need to force vim to quit, use `:q!`
* If you've made changes and want to save before exiting, use `:wq`. Again, if there's something wrong and you need to force vim to quit, use `:wq!`

### Other ways to create and edit C Source code
* **VSCode**
    Free and powerful editor from Microsoft
* **CLion**
    Powerful C/C++ IDE(Integrated Development Enviroment) from JetBrains, easy to get free student license.
* **Visual Studio**
    Powerful C/C++ IDE from Microsoft, free community version, (Quasi?)-Best IDE for Windows Users.
* **CodeBlocks**
* **QtCreator**
* **Sublime Text**
    Light weight editer
* **Atom**
    Light weight editer
## Compile and execute a program 
### Use GCC to compile your C source file

* Return to the terminal.
* Compile the C program using GCC:
    ```bash=
    gcc my_program.c -o my_program
    ```
    this command compiles the source code into an executable file named "my_program."

    I believe gcc in preinstalled in iLab. You could check by this command:
    ```bash=
    gcc --version
    ```

* To run the compiled program, use:
    ```bash=
    ./my_program
    ```
#### Additional GCC
* `-Wall`: Enables most warning messages, which can help you catch potential issues in your code.
* `-g`: Generates debugging information, which is useful when debugging your program with a debugger like GDB.

    For example, to compile with warnings enabled and debugging information, you can use:
    ```=
    gcc -Wall -g my_program.c -o my_program
    ```
### CMake(Optional but Recommended for Large C and C++ projects)
In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method. CMake is not a build system itself; it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.(Reference: CMake. (2023, September 26). In Wikipedia. https://en.wikipedia.org/wiki/CMake)


## Basic Linux Commands

* Check where you are now
    ```bash=
    pwd # stands for Print Working Directory
    ```
* Rename and remove files
    ```bash=
    mv my_program.c test.c
    rm test.c
    rm -rf *
    ```
    Note that the options `-rf` are two separate options:
    * -r: remove file **recursively**. You can skip this option if you are only remove a single file, but remember to keep it if you want to remove a folder.
    * -f: **force** to remove. Be cautious to use this option. Once you delete something with flag -f, it's gone forever. Think twice before you use this flag, especially when you are trying to remove some system libraries and such.

* Another way to simply create a file
    ```bash=
    touch foo.txt
    ```
* Display the content of a file to command line:
    ```bash=
    cat foo.txt
    ```
* Write someting to a file from the command line
    ```bash=
   echo "hello world" >> foo.txt
   cat foo.txt
   # hello world
   echo "hello world1" >> foo.txt
   cat foo.txt
   # will display:
   # hello world
   # hello world1
    ```
    ```bash=
    echo "hello world2" > foo.txt
    cat foo.txt
    # hello world2
    ## everything gone but only hello world2 left.
    ```
    Note that `>>` here means append string "hello world" to the text file `foo.txt`. If you want to clear the entire file and append something new to the file, then use `>`.
