Simple demo: basic programming tools in linux
NOTE: Stuff between () and in CAPS should not be typed. Stuff in CAPS is
a key; for example ENTER means hit the ENTER key and CTRL- means hold down
the Ctrl key.
-
Open a shell (click on button with little screen and "shell" next to
"start" button).
-
Now type:
pwd ENTER
-
Remember that ENTER means that you should press the ENTER key.
-
You should see something like:
/class/csld/cs12/cs12ab
-
Where "cs12ab" is replaced by your login name.
-
In unix when you are given an account on a machine you are also given
a directory where you can store files. This directory is usually given the
same name as your login. This directory is sometimes called your
"home" directory because each time you start a shell you will be
started in that directory.
-
To see the contents of your home directory type:
ls ENTER
-
You should see
winnt/
-
For each project that you create in this lab you will need to create
a new directory inside your home directory.
-
To make a directory for this demo project type:
mkdir demo1
ENTER
-
then type:
ls ENTER
-
You should now see:
"demo1/ winnt/"
-
Notice that "demo1" is now listed. Now type:
cd demo1 ENTER
-
This will change you into the directory you have just created.
-
We will now write and compile a simple c++ program.
-
Type:
emacs Hello.cc & ENTER
-
Emacs is a highly advanced programmer's editor that has many features
that make progamming easy. Many professionals use emacs or similar editors
to edit their code.
-
Enter in :
#include <iostream> ENTER
ENTER
int main(){ ENTER
std::cout "Hello World\n"; ENTER
} ENTER
-
Notice as you were typing, that C++ keywords were highlighed and that
pressing TAB on a line will align the code for you in accordance with some
good coding style. These are some of the many valuable features of emacs.
Coding style is very important. We will explain more about coding style and
why it is so important later labs.
-
After you have finished typing, double check your work to make sure
you have made no typos. Checking for typos will save you time in the long
run.
-
Now type "CTRL-x s" to save your file. Remeber this means
that you hold down the Ctrl button and then press the X key at the same
time. After that you press the s key. Your file is saved in the directory
that the shell was in when you started emacs. By looking at the steps above
can see that we were in the "demo1" directory.
-
Now open another shell. Notice that you are started in your home
directory again and that you need to change to the demo1 direcory in order
to be able to see Hello.cc
-
Type:
cd demo1 ENTER
-
To change to the demo1 directory:
ls ENTER
-
You should see Hello.cc. If you don't please let the your TA know.
-
Now you will compile your code to make an executable. Type:
g++ -Wall -W -Werror Hello.cc -o hello ENTER
-
You should see a compile error. I purposefully inserted an compile
error above in order to illustrate the compile-debug cycle.
-
Switch to emacs
-
Fix the error in emacs (insert << before "Hello World")
-
Now you need to save your changes so type:
CTRL-x s ENTER
-
Switch back to your shell
-
Type:
g++ -Wall -W -Werror Hello.cc -o hello ENTER
-
Notice that your compile error has gone away. You progam has now been
created with the name "hello"
-
Type:
./hello ENTER
-
You should see:
Hello World
-
Exit from emacs by typing "CTRL-x c" (and ENTER).
-
Type:
ls ENTER
-
Notice that you see a list of files similar to:
hello* Hello.cc Hello.cc~
-
Your executable is colored green and has a "*" at the end
in order to signify that it's an executable. Notice that in unix you don't
need to have a file end with ".exe" in order for it to be an
executable.
-
To change the name of a file you need to use the "mv"
command
-
For instance, to change the name of your executable from hello to
say_hello type:
mv hello say_hello ENTER
-
After typing the line above you can type:
./say_hello ENTER
-
in order to run the program again.
-
Unlike windows, in unix case also matters. This means that typing:
./Say_hello ENTER
-
Will not work because the case does not match. You have to type
exactly:
./say_hello ENTER
with lower-case letters.
-
You are now ready to compile and edit programs in most unix
environments. The computer science curriculum at UCR uses linux extensively
in undergraduate and graduate classes. For more information on using emacs
and the linux enviroment see the links in the class syllabus.
© 2003 Anwar
Adi. All rights reserved.