Overview of Lab

After this lab you should be able to:

Back to Schedule

Exercise

Quick Links:

 

  • Unix Commands
  • Compiling
  • Sequential Search
  • Selection Sort
  • Insertion Sort
  • Binary Search

We will leave the explanation of compiling files until a later lab. Let's learn by example:

Single Files

Compiling a Single File

You might want to perform these in your CS115 directory. You can use the ls command after step 1 and 2 to see what files are being added to your directory.

  1. First, get the file to be compiled:
    • cp /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp hello.cpp
  2. Then, compile:
    • g++ -o hello hello.cpp
  3. You can now run the code:
    • ./hello

Explore:

  • Try playing around with the order of the arguments after the g++ in Step 2
    • If something happens to your hello.cpp code, don't worry--you can always get it again (from Step 1)

  • Try leaving out the -o hello in Step 2. What is produced? Can you use that to run the code?

  • Try replacing hello with test. Try to run test with or without the ./ in front. Does hello work that way too? If you are curious about why, try man test
Multiple Files

Compiling Multiple Files

You might want to perform these in your CS115 directory. You can use the ls command after step 1, 2, and 3 to see what files are being added to your directory.

  1. First, get the three files to be compiled:
    • cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp .
    • cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp .
    • cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h .

  2. Then, compile the two .cpp files:
    • g++ -c main.cpp
    • g++ -c myFunction.cpp

    • What two files are created? These are refered to as object files and contain the machine code

  3. Now, the two object files need to be "linked" or combined together into the "executable" (in other words, the file that will be run)
    • g++ main.o myFunction.o -o main

  4. You can now run the code. What will you type?

Notes: