The key to succesfully finishing labs is reading the instructions carefully.
Make a good habit of reading every word -- don't skip any text.
The system setup page and resources page are your starting points for help on how to use the computing environment and get things done for this class.
Of course, you should have already followed these instructions to set up your Ubuntu environment on your laptop. If not, now is the time to do it!
The computers in the lab are also running Ubuntu! To get them set up with your ic210 directory, and the submit script that will be used to submit your labs, just like in WSL, follow these instructions. To open a terminal on an Ubuntu machine (like the ones in the lab), you can click Ctrl+Alt+t.
The way you open these text editors is a bit different: For Atom (which runs both on Windows and Linux), you open Atom from your start menu, then you navigate to your ic210 folder to create and edit files.
If you're using gvim, emacs, or geany you can open your text editor by running a command in the terminal directly. This can be pretty convenient, since you already have to have a terminal window open to compile and run your programs.
For example, to open or create a file hello.cpp using geany, assuming you are already in the ~/ic210/lab01 folder, you could just type:
geany hello.cpp &
Notice the & at the end - this makes sure geany doesn't "take over" your
terminal window and you can keep using it while geany is open. Replace "geany"
with "gvim" or "emacs" to use those programs instead. Note that gvim is awesome and actually doesn't require you to use the &.
If you're using VI (i.e., gvim), start with this quick intro and list of useful commands. If
you're using emacs, check out this getting
started guide. The resources
page also has further references to help you learn the powerful features of these text editors. (You should be able to get started in atom or geany without any extra help.)
wsl from the start bar. In Ubuntu labs, that
means clicking Ctrl+Alt+t, or selecting Terminal from the menu.
m012345@WK2ESUIO4MID:~$
This is actually telling you some useful information! In this example:
m012345 is your username (should be the same as your USNA
username)WK2ESUIO4MID is the name of your computer
(sorry, ITSD doesn’t like to give a nice looking name to your
laptops)~ is the current directory. A tilde
~ indicates your home directory, which is
the base of where all your personal files will be stored in
Linux.Type
ls
to see the contents of your current directory. You
should see something like
Desktop Downloads bin ic210
The Desktop and Downloads are links to those
folders in Windows, and ic210 is a link to the exact same
folder called ic210 on your Desktop.
ic210 directory is the place to be! To move into that
directory type cd ic210
Notice that your prompt changes to indicate you are now in a different directory!
It's a good idea to always start and end any period of work that you do, by syncing up with git. More detail on this below, but for now you just need to run:
210syncmkdir lab01 cd lab01
lab01 folder that is under
ic210, and create (and save) a file called
main.cpp.
gvim main.cpp&(Try and learn a bit about Vim: Vim cheatsheet, the interactive Vim tutorial, Vim Introduction and Tutorial)
emacs main.cpp &(Try and learn a bit about emacs: Emacs Essentials, a bigger list of commands, a much bigger list of commands)
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
When finished, save your file.
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,
g++ main.cpp -o m012345 | (Of course, use your alpha instead of m012345.) |
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 lsThe 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 below 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."
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.
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.
Repeat Part 3 of this lab all over again, with these changes:
add.cpp instead of
main.cpp and make sure you replace first line comment with your actual name and alpha):
| add.cpp | sample run |
|
~/$ ./add
The sum of these two integers is 25
|
add.
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
std::cout << "This is a 1!" << std::endl;
return 0;
}
|
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!
It's almost a puzzle to use as few keystrokes as possible. In particular, what editor commands would do the following?
|
cd ~/ic210/lab01
~/bin/submit -c=IC210 -p=lab01 lab01.cpp
If you did not get the output 100% correct, the page should give you an indication of what's wrong with your output.
You'll be writing a lot of code this semester. It would be a shame if anything happened to it!
Run 210sync to save your work in github.
Remember to run 210sync every time you do some work, either on your laptop or on your lab machine. Also make sure you always do your work in your ic210 directory!!
.