Lab 10

Notes


Second Bonus

A quick hello to shells, C++ and makefiles.

Part I: Introduction to Terminal Shells

Below are some useful shell commands that you may need for today's lab:

ls   				:list files and directories in the current directory
mkdir XXX     			:make a new directory called XXX
cd XXX       			:move into sub-directory XXX
cd        			:cd by itself takes you back to your home directory.
cd ..          			:go up one directory
svn checkout https://..... YYY	:Checkout project to YYY directory
svn commit -m 'message'        	:Commit changes in the current directory 
svn help

For example, the following will create a directory "red" on your machine and it will contain a copy of all the files and directories that are stored on the subversion server's 'luckydog' directory.

svn checkout https://blah.com/abc/luckydog red

Part II: Checking out the project and manually compiling

Getting started:

  1. Login to a lab linux machine.
  2. Open a Terminal ('shell') window (under the 'Applications->Accessories' menu). Note you can paste into a terminal window by right-clicking.
  3. Get the project from subversion. Carefully type or copy-paste:
    svn checkout https://subversion.ews.illinois.edu/svn/sp17-cs125/YOURNETID/Midterm2Bonus
    Be sure to use your own netid and make sure there are no extra spaces. Watch out for upper/lowercase.
  4. Now you have a local copy of all of the files (hello.cpp, Makefile and this readme). Change the current directory and list the files.
    cd Midterm2Bonus            (type "cd Midterm2" then press tab to autocomplete)
    ls
  5. You should see hello.cpp and a couple of other files.
  6. You can compile the project manually by running the compiler for each source file. For every .cpp file, type the following to create the binary machine code (the '.o' files):
    g++ -c -g -O0 -Wall hello.cpp
  7. Finally, link all of the libraries and binary .o files into a single executable file:
    link all of the libraries and binary .o files into a single executable file
  8. This is the command to link all libraries and .o files
    		
    g++ -o exec file1.o file2.o file3.o -lm

    In our case it would be
    g++ -o hello hello.o -lm

  9. To run the new program, type the following:
    ./hello

Part III: Using 'make' to automatically compile and link the project

This MP introduces you to C++, compilers and makefiles, things you'll be using in CS225. In CS125, Eclipse compiled your java source files automatically. In CS225, you'll need to run the C++ compiler yourself.

C and C++ programmers often use 'make' to build the software. For example, the linux kernel is compiled by typing 'make'. You specify the build rules (dependencies) in a 'Makefile'. 'make' checks the existence and modification times of files, compiling only when necessary. Type 'make' a few times, and if you haven't made any changes to any source files, then make will not recompile them. On some systems it will report:

make: Nothing to be done for `all'.

By the way, 'make' is a very general purpose tool. You can specify how a specific output file (or file extension) depends on an input file. If you update a file, or add new files, make will automatically recreate the output files, assuming you've set the rules correctly. For example, I've used it to generate thumbnails of recently added jpgs.

Remember, to see your changes you need to:


Part IV: Editing files, and committing changes back to subversion

Now you'll be changing hello.cpp and you'll need to re-compile. Before we do that, let's examine how to compile the code using 'make'.

  1. Open the hello.cpp file in an editor (e.g. Applications->Programming->Vim improved).
  2. Make a tiny change: change 'first' to 'second'.
  3. Save the file.
  4. Commit your change to subversion:
    svn commit -m 'blah blah'
    • Note that 'blah blah' can be any message you want (it's just there for other programmers to read). A typical message might be 'fixed xyz bug' or 'added new feature xyz'.
    • The single quotes are required. "-m" means the log message follows.
  5. Watch as hello.cpp file is sent back to subversion. If nothing happens:
    • Did you save your change to hello.cpp?
    • Ask the lab assistant for help.
  6. Let's see your program run! Remember, you need to compile it and run it:
    make
    ./hello
  7. If you have a syntax error in your code, then 'make' will tell you where the error is.

Part V: Modifying hello.cpp


Part VI - Final Words

Obviously, this is a very brief introduction to some very powerful tools and concepts. Google and lab assistants are your friends if you'd like to know more!

If you have any spare time you could try running your c++ program using the debugger:

Type: 
gdb hello
l
break 10
print mesg
run
continue
quit
 
-----------------------
 
#include 
#include 
 
using std::string;
using std::cout;
using std::endl;
 
int main()
{
   cout << "Hey,";
   string mesg = "this my first C++ program";
   mesg += "!";
   cout << mesg << endl;
   
   for(int i=0; i<10; i++) {
     cout << i*i;
     cout << endl;
   }
   return 0;
}