It is the purpose of this exercise to walk you through the basics of using the NIU Computer Science Department's Unix servers to develop and hand in your programming homework assignments.
Before you can do this training exercise, you must connect to Unix and activate your departmental Unix account.
If you're using Windows and want to download and install the PuTTY SSH client, follow the instructions found here. Note that users of Windows 10, macOS, and Linux already have access to command-line software that can connect to the departmental Unix servers. Downloading and installing PuTTY is optional (but recommended)for Windows 10 users.
Follow the instructions on how to connect to Unix and activate your departmental Unix account.
csci501
directoryCreate a suitable directory within which to implement your assignments.
Make sure you are in your home directory. At the Unix prompt, type the following command and press [enter] to execute it.
cd
Create a csci501
directory.
At the Unix prompt, type the following command and press [enter] to execute it.
mkdir csci501
Navigate to the csci501
directory by executing the following command:
cd csci501
Notice how the working directory name displayed in the command prompt changes?
It's also possible to copy a command like this from a web page and paste it into the PuTTY terminal window. Select the command in the web page, type Ctrl-c to copy the command text, click on the PuTTY window to give it the focus, and then right-click to paste the command.)
ls
Read through the following three web pages to get some background on the Unix operating system:
Navigate to the directory created for this course by by typing the command:
cd ~/csci501
Use the ls
command to list the names of the files and subdirectories in your working directory:
ls
Use the mkdir
command to create a new directory for this assignment:
mkdir Assign1
Use the ls
command again to list the names of the files and subdirectories in your working directory:
ls
Use the cd
command to change to a new working directory:
cd Assign1
Use the pwd
command to print the absolute pathname of your working directory:
pwd
List the names of files in your working directory:
ls
Use the -a
option to see "hidden" files (file names that begin with .
):
ls -a
Use the -l
option to see a "long" listing of more information about the files:
ls -l
(You can read more about the information displayed by the -l
option here.)
You can also combine the two options:
ls -al
Change your working directory to the parent directory:
cd ..
(That's "period period", which means "parent directory.")
Use the cp
command
to copy some files from olddir
to newdir
:
cp olddir/file1 olddir/file2 newdir
cp olddir/.anotherFile newdir/.anotherFile
List the contents of newdir
to verify that the copy has taken place:
ls -al newdir
Use the mv
command
to rename .anotherFile
so that it is no longer "hidden":
mv newdir/.anotherFile newdir/anotherFile
ls -l newdir
You can also use mv
to move a file from one directory to another:
mv newdir/anotherFile olddir
ls -l newdir
ls -l olddir
Use the rm
command
to remove the files in newdir
:
rm newdir/file1 newdir/file2
Once a directory is empty, it can be removed with the
rmdir
command:
rmdir newdir
(You can also use the rm
command with the -r
option to remove a directory and all of the files and subdirectories within it. Be very careful when doing this! rm
is unforgiving and can not be "undone!")
rm -r newdir
Open a text editor and create a file called labtrain.cpp
. For beginners, the easiest editor to use would be nano. (While nano is easy to learn, it is cumbersome to use. The vim editor is harder to learn but is more efficient to use to write programs. emacs takes this a step further.) To open the editor and create the new file, execute the command:
nano labtrain.cpp
Type in the following program:
//****************************************************************** // // CSCI 501 Assignment 1 // // Author: your name // // Unix Training Exercise // //****************************************************************** #include <iostream> using std::cout; using std::endl; int main() { cout << "Hello World!" << endl; return 0; }
Save the file by executing Ctrl-O.
Exit nano by executing Ctrl-X.
Compile and link the program by executing the command
g++ -Wall -Werror -std=c++11 -o labtrain labtrain.cpp
If there are any errors, open the labtrain.cpp
file in nano and make the necessary fixes.
Then run the previous command again.
Once your program has compiled and linked successfully (no error messages), you can run it by typing the following command:
./labtrain
Using the g++
command to compile and link your program repeatedly can involve a lot of typing, especially if you have multiple source code files (which is the norm). Also, if you have multiple source code files, you probably don't want to recompile all of them every time - typically, you only want to recompile the files that have been changed since the last time you compiled your program. Makefiles and the make
utility are a way to avoid these issues. You can read more about make
and makefiles in the relevant section of the Course Notes.
Create another file, this time called makefile
by executing the
command
nano makefile
Type in the following code.
IMPORTANT NOTE: The lines that begin with g++
and rm
MUST be indented by using a tab character, not spaces.
# # PROGRAM: Lab Training Exercise # AUTHOR: your name # CXXFLAGS = -Wall -Werror -std=c++11 labtrain: labtrain.o g++ $(CXXFLAGS) -o labtrain labtrain.o labtrain.o: labtrain.cpp g++ $(CXXFLAGS) -c labtrain.cpp clean: rm -f *.o labtrain
Save the file by executing Ctrl-O
Exit nano by executing Ctrl-X
Execute the command make
at the command prompt. This will use the make utility to compile and link the labtrain
program. At this point, you shouldn't have any errors in labtrain.cpp
, but you may need to fix mistakes you made in your makefile. For example, if you get an error message that says "*** missing separator. Stop.", that means you've indented your makefile using spaces rather than the required tabs.
Run the program by typing the following command:
./labtrain
If the program runs successfully, execute the command make clean
at
the command prompt to remove the executable file and the object file (labtrain.o
).
This step is necessary because these two files can become quite large.
To hand in your assignment you must follow the instructions in the Assignment Submission Instructions.
As discussed on in the Submission Instructions, for this assignment you would use this sequence of commands:
cd ~/csci501/Assign1 make clean cd .. mailprog.501 Assign1
Answer the questions when prompted by the mailprog.501
script to specify the assignment number (in this case, 1) and, if requested, the section number
for the course within which you are enrolled. (Some classes only have one section and thus you might not
be asked to provide it.)
You can check that your assignment was submitted by following the instructions in the Assignment Submission Instructions.
DO NOT try to submit your assignments using blackboard. DO NOT submit your assignment by mailing it to your TA or instructor. Any Such submissions will not be accepted.
When you are done with a Unix session, you can log out of the system by typing the command
logout
Last modified: 2022-01-18 11:25:59 CST