Part 1: The Computing Environment
Login: Ubuntu
- Your username and password are the same as those of
your MIDN account. Enter your username and password to log
on.
Note: If you're reading this in lab, you've probably already done this
step!
Your Unix Account
- You already have an account on the CS Department's Unix
machines, which are actually running GNU/Linux.
- The Unix environment is quite different than Windows and
will take some getting used to. I strongly recommend
doing all of your programming work on the Unix machines
so that you comfortable with it by the 6-week exam. A quick
reference guide to common Unix commands is provided on the
Resources page.
- You are always welcome to work in the Unix labs
throughout the semester; however you must also get comfortable
with some other option that will work from your room. For example,
- Use VMware to emulate Ubuntu Linux on your Windows machine (highly recommended).
See the Resources section of the IC210 home page for info on how to set these up.
Part 2: Editor and project
Choose your editor
We will be using the Unix environment to create, compile&link and
execute C++ programs. First, you must choose a text editor to create
your first C++ program. Sounds easy, right? Wrong! Your
choice of text editor will mark you for life. Choose wisely!
- emacs - feature-rich and powerful text editor.
←Course coordinator's recommended choice!
- vi/vim/gvim -
lightweight and powerful text editor.
← Many like it
- atom -
a new pretty-to-look-at editor (written in Javascript)
Part 3: Step-by-step instructions to creating the "HELLO WORLD" program
-
Directory setup.
- Edit the source code
Explore vim |
Explore emacs |
Try and learn a bit about Vim using the below references:
|
Try and learn a bit about emacs using the below references:
|
Open your preferred text editor. For example, if you use
vi, in your terminal window type
vi main.cpp
or if you use emacs,
emacs main.cpp &
Now use your text editor to copy in the following source code into the main.cpp file:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
When finished, save your file.
- Compile (and link).
If you want the programs you write to actually do
something then you must compile them (turn source code into machine language).
Converting high-level C++ source code into machine language is very complex,
but compiler vendors solve this problem for you. To compile your program, first
make sure that you have a terminal open from before, whose current directory is
where you saved main.cpp. Once you are there, compile by typing into the
terminal
g++ main.cpp -o m012345
There should be a new file in your working directory called m012345.
(Use your alpha instead of m012345.) Check this by typing:
ls
If you omit the "-o m012345", which we will often do, then a file
with default name of "a.out" is created. Try:
g++ main.cpp
ls
The new files m012345 and a.out are the "executable file" for your program.
If you do see any error messages from g++
, then get assistance
from the instructor. Tips for deciphering error messages are given at the end
of this tutorial.
Note: technically, the single command you issued actually "compiled"
and "linked" your program. We'll talk much later about this,
but in short "linking" (amongst other things) takes your code and combines it
with other code provided elsewhere to do standard things like input and output
(this is referenced by the #include <iostream> line in your
code). Often, we'll informally call this whole process "compiling."
-
Run!
Once you successfully compiled and linked your program
(no errors), then you are ready to execute your program. In your current
working directory, you will see a new file called m012345
(or
whatever you have named it). That is your program. To execute a file, type the
file's name preceded by a "dot slash" (literally, "./") and hit the enter key.
Note: the "dot slash" is telling the computer to look for the program (m012345)
in the current directory (indicated by the "dot"). In some cases you can get
away with not including this, because your computer is set up to automatically
look for commands in the current directory (amongst other places), but
including the "dot slash" is safer because it will always work.
- Program Output:
Your program will output to the same
terminal from where it was executed, as seen above.
Now, let's view the executable file you just executed. Type
ls -l
to list details of the contents of your current working directory.
There is a file named main.cpp – it is your source code file. Note the
size of the file (95 byes in our example). The file called m012345 is your
program's executable file, which is combined with all of the other necessary
machine code to run your program. Note the size (7782 bytes in our example)!!
The increase in size is due to the code that was “included” or
linked into your program.
Part 4: Let's do it all over again!
You just accomplished quite a bit; you typed in a C++
program, compiled it and executed it. It is important that
you understand all the steps involved, and that you are
comfortable using a text editor and the terminal.
Repeat Part 3 of this lab all over again, with these changes:
- First, close all open windows (including the editor and terminal).
- In step 1, you've already created directories (mkdir) and change
permissions (with chmod). You still need to open a terminal and "cd" to the
correct directory for lab01.
- Name your source file
add.cpp
instead of
main.cpp
. Now, what do you need to change when you compile your
program?
- Instead of entering the "Hello World" program, enter in this
simple addition program:
add.cpp | sample run |
// (Your name and alpha)
// This program adds two numbers
#include <iostream>
using namespace std;
int main()
{
int number1, number2, sum;
number1 = 12;
number2 = 13;
sum = number1 + number2;
cout << "The sum of these two integers is "
<< sum << endl;
return 0;
}
|
~/$ ./add
The sum of these two integers is 25
|
Compile your program giving the executable the
name add
, and run it, making sure it prints out the
right answer! Fix it if it doesn't!
Part 5: Fixing errors in compilation
How to read the error message
When you make certain kinds of common mistakes (
syntax errors), the
compiler won't be able to understand your program and will issue error
statements in the terminal window.
- Each error message includes a number, which
represents the line number of the error. Go to that line number to see where
the problem is.
- Every syntax error must be corrected before a program will
compile. Sometimes it is clear what's wrong, and other times you may have a
hard time figuring out how to correct a syntax error.
Tips
You will improve as you see enough errors to associate the messages with the
syntax error. Here are a few simple tips:
Errors on purpose: Don't skip this part!
Experience and practice help a lot here. So for each of the below,
purposefully make the syntax error in your current program and try to compile
to see what the error message is. Correct the error before introducing the
next error into your program (e.g., only add ONE ERROR at a time). Go slowly
and pay attention, this will save you time later!
- remove a semi-colon.
- change a "<<" to a ">>".
- change the variable number1 to Number1.
- remove the ending }.
- remove the "#include <iostream>" line.
- remove the "using namespace std;"
Part 6: Using Your Laptop
You're going to be writing and running a lot of programs this
semester. Therefore, the following questions are important:
- Where do you do write code?
- Where do you store your code?
We'll take some time in class to address these questions.
-
Where to work.
You will be writing and running your programs
- either on the CS Department Unix lab machines,
- or on the VM (virtual machine) you've installed on your
laptop.
You will probably want to bounce back and forth - work on something in the lab,
continue work on your laptop VM later.
-
Where to store your work. You have three basic places to store files.
- Your laptop's filesystem (i.e., Windows).
Storing stuff on your laptop's Windows filesystem is not a very convenient
option because the writing, compiling and running will be happening either on
the CS Department Unix lab machines or on your VM. Anything on your laptop's
Windows filesystem would have to be constantly copied back and forth.
- Your VM's filesystem.
Storing stuff on your VM's filesystem is nice in some ways
- you can work regardless of whether or not you have network access, for
example. On the other hand, it's got some serious negatives. If your laptop
dies you lose all your work. If the file on your laptop that contains the VM's
state gets corrupted, you lose all your work. Finally, the work you do in the
lab is not available on your VM and vice versa, unless you go back to
constantly copying files back and forth.
- Our recommendation: your CS Department home directory.
It turns out that storing stuff in your CS Department home directory is the
best option.
- The lab machines use this storage; when you log in with a lab machine, you will see your files.
- It's also accessible from your VM, as long as you have a network connection,
thanks to the "csunixmount" script that's preloaded on your VMs.
Use the csunixmount
script and keep
everything in your CS Department home directory.
csunixmount, csunixunmount (in VM)
Bring out your laptop, pull up VM, and then pull up a terminal.
- In your VM terminal, give the command:
ls
Note the
directory "csunix". Give the command cd csunix
then ls
Note that it's empty. Give the following command to go back to your home
directory cd ..
- Run
csunixmount
Now cd to csunix
again cd csunix
and do an ls: ls
You will see the
files you worked on in your the lab machine.
- Give the command:
cd ~
Note ~ means your home directory in VM.
Give the command csunixunmount
and
do ls csunix
Now it's empty again.
Give the commands:
csunixmount
ls csunix
Files are back!
csunixunmount before pausing your VM
If you "pause" your VM before exiting VMPlayer (as opposed to shutting
the VM down), we recommend you run the csunixunmount
script first.
Passwordless mounting (in VM)
When you run csunixmount it asks you for your network password because you
are connecting to a USNA server. We can skip the password requirement by
using public/private keys (remember SY110?). You generate a key pair in your
VM, keep the private key in the VM, and send the public key to the USNA
server. Once you do this, future ssh or csunixmount will not prompt for a
password.
cd ~
ssh-keygen -t rsa
When asked where to save the keys, just hit ENTER to accept the default
location. When asked for a password, just hit ENTER twice to skip that
requirement.
You now have a key pair in your ~/.ssh folder. Let's copy it to the lab
machines:
scp .ssh/id_rsa.pub csmidn:
And now we login to the lab server ourselves for final setup:
ssh csmidn
Copy the public key to the authorized keys file:
mkdir -p .ssh
touch .ssh/authorized_keys
cat id_rsa.pub >> .ssh/authorized_keys
exit
Done! Now you can csunixmount with ease!
Part 7: More on Linux (Do this part with your laptop)
You'll spend the rest of your time learning more about Linux and the more
powerful editors emacs/vi.
- Review the Unix command summary. Then,
open a terminal and practice entering all of the "Usage Examples" (from
the "Unix command summary" page) to see what each command does (the
commands need to be done in order). Use "
ls
" frequently to
see what effect the commands are having.
- In your VM, run csunixmount (if you haven't done so yet), and cd to
~/csunix/ic210/lab01.
- Create lab01.cpp as follows:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "This is a 1!" << endl;
return 0;
}
-
Modify the program lab01.cpp so that it's output matches what's shown
exactly!
Use editor commands: Your focus is on using the editor to make these changes as
efficiently as possible! In other words, of course you can do it, I want
you to concentrate on doing it smart!
Use editor commands! It's
almost a puzzle to use as few keystrokes as possible.
In particular, what editor commands would do the following?
- Copy a line
- Paste
- Delete (or replace) a word
required output |
Hello World!
Hello Solar System!
Hello Galaxy!
Hello Universe!
Hello ... ?
This is a 1!
This is a 2!
This is a 3!
This is a 4!
This is a 5! |
Part 8: Submit this lab01.cpp to the submission system
TEST TEST TEST your code before proceeding
- BEFORE you submit your code, look very carefully at the required output (above), and compare it to your output. Does it match exactly?
Throughout this course, it will be very important that you TEST your code to see if it works properly. Normally, that will mean (a) testing it on a
variety of inputs and (b) ensuring that it produces precisely the desired output in each case. Right now, your program always does the same thing (there is no "input"),
so you just need to verify that the output is correct. "Correct" output means having exactly the right spacing, "newlines", capitalization, punctuation, etc.
In your Laptop
- (Install the submit script)
Follow this instruction in the
resources to install the submit script.
-
Cd to the directory that contains the lab01.cpp file.
cd ~/csunix/ic210/lab01
-
Submit your program by giving the following command:
~/bin/submit -c=IC210 -p=Lab01Laptop lab01.cpp
-
Using your web browser, check the submission server and check your work. If
you did not get the output 100% correct, the page should give you an indication
of what's wrong with your output. Keep fixing your program and resubmitting
until it works perfectly.
- Note that for some IC210 assignments, the "submit" system will only allow you to make a limited number of submissions. This will be to encourage you to check
your program carefully (test it yourself) before you submit.