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.

  1. Open a shell (click on button with little screen and "shell" next to "start" button).
  2. Now type:
    pwd ENTER
    
  3. Remember that ENTER means that you should press the ENTER key.
  4. You should see something like:
    /class/csld/cs12/cs12ab
    
  5. Where "cs12ab" is replaced by your login name.
  6. 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.
  7. To see the contents of your home directory type:
    ls ENTER
    
  8. You should see
    winnt/
    
  9. For each project that you create in this lab you will need to create a new directory inside your home directory.
  10. To make a directory for this demo project type:
    mkdir demo1
    ENTER
    
  11. then type:
    ls ENTER
    
  12. You should now see:
    "demo1/ winnt/"
    
  13. Notice that "demo1" is now listed. Now type:
    cd demo1 ENTER
    
  14. This will change you into the directory you have just created.
  15. We will now write and compile a simple c++ program.
  16. Type:
    emacs Hello.cc & ENTER
    
  17. 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.
  18. Enter in :
    #include <iostream>             ENTER
                                    ENTER
    int main(){                     ENTER
      std::cout "Hello World\n";    ENTER
    }                               ENTER
    
  19. 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.
  20. 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.
  21. 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.
  22. 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
  23. Type:
    cd demo1 ENTER
    
  24. To change to the demo1 directory:
    ls ENTER
    
  25. You should see Hello.cc. If you don't please let the your TA know.
  26. Now you will compile your code to make an executable. Type:
    g++ -Wall -W -Werror Hello.cc -o hello ENTER
    
  27. You should see a compile error. I purposefully inserted an compile error above in order to illustrate the compile-debug cycle.
  28. Switch to emacs
  29. Fix the error in emacs (insert << before "Hello World")
  30. Now you need to save your changes so type:
    CTRL-x s ENTER
    
  31. Switch back to your shell
  32. Type:
    g++ -Wall -W -Werror Hello.cc  -o hello ENTER
    
  33. Notice that your compile error has gone away. You progam has now been created with the name "hello"
  34. Type:
    ./hello ENTER
    
  35. You should see:
    Hello World
    
  36. Exit from emacs by typing "CTRL-x c" (and ENTER).
  37. Type:
    ls ENTER
    
  38. Notice that you see a list of files similar to:
    hello* Hello.cc Hello.cc~
    
  39. 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.
  40. To change the name of a file you need to use the "mv" command
  41. For instance, to change the name of your executable from hello to say_hello type:
    mv hello say_hello ENTER
    
  42. After typing the line above you can type:
    ./say_hello ENTER
    
  43. in order to run the program again.
  44. Unlike windows, in unix case also matters. This means that typing:
    ./Say_hello ENTER
    
  45. Will not work because the case does not match. You have to type exactly:
    ./say_hello ENTER
    
    with lower-case letters.
  46. 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.