HOWTO: Create and compile programs on cs-class-1

  1. Log on to class-1.cs.georgetown.edu using either SSHWin on Windows or ssh on MacOS or Linux. The text in your window should look like the following:

  2. To create a program, perform the following steps:

    1. Open an editor and create a program. To use the nano editor type at the command prompt: nano <program name>.

    2. As an example, type: nano first.cc

    3. This will create an empty file named first.cc.

    4. To get help in nano, type ^G by holding down the Control key and pressing "G".

    5. Enter the following program:
      // A first C++ program that sums two numbers.
      
      #include <iostream>
      
      using namespace std;
      
      int main()
      {
        int x, y, sum;
      
        y = 5;
        x = 6;
        sum = x + y;
        cout << "The sum is " << sum << endl;
        return 0;
      } // main
      
    6. Once you have entered the program, type ^O to save your file. The text in your window should look like the following:

    7. Exit nano by typing ^X, which will take you back to the command prompt.

    8. One important command to know for nano is ^_ (Control-Underscore), which takes you to a specific line in the file. If there is an error in your program, the compiler often report the line on which the error occurred. For large programs, it is a necessity to be able to go directly to a line, rather than scrolling or paging to the line.

    9. NOTE: If you plan to do some more programming beyond this course, then I recommend that you learn and use the vi editor or the emacs editor. To get help for vi, at the UNIX prompt, type man vi, which will display the on-line manual page for vi. There is also an on-line vi Reference Card. There is also an online book entitled Learning the vi Editor, which available through the library's subscription to Safari Books Online. Similar resources exist for emacs. To use vi type: vi <program name> (e.g., vi first.cc). To use emacs type: emacs <program name> (e.g., emacs first.cc).

  3. To compile a program under UNIX, perform the following steps:

    1. At the UNIX prompt, type g++ first.cc

    2. If there are errors in your program, then they would be displayed, and you'll need to edit the program to fix the errors.

    3. If you entered the program correctly, then you will be returned to the UNIX prompt.

    4. The compiler creates an executable file called a.out, which is the default name for executables. You can view the files in your directory using the ls command.

    5. To run the program simply type a.out at the command prompt. (If the system responds that the command was not found, then you need to type "./a.out", without the quotes.)

    6. The program should produce the following output:
        The sum is 11
        
    7. You can rename the executable by typing mv a.out first.exe, which renames a.out to first.exe.

    8. You can also specify the desired name of the executable at compile time by typing g++ -o first.exe first.cc. The -o option specifies the name of the executable file.

    9. A transcript of this session follows. Boldface type indicates things that the user typed. Comments appear after each command.
      cs-class-1%                          <--- the command prompt (yours might look different)
      cs-class-1% ls                         <--- lists the directory, which is empty
      cs-class-1% nano first.cc              <--- opens nano with the empty file first.cc
      cs-class-1% ls                         <--- lists the directory, which now contains the new file
      first.cc
      cs-class-1% g++ first.cc               <--- compiles the program to a.out, the default name
      cs-class-1% ls                         <--- lists the directory, which now contains the compiled program a.out
      a.out*  first.cc
      cs-class-1% ./a.out                    <--- executes the program a.out
      The sum is 11
      cs-class-1% mv a.out first.exe         <--- renames a.out to first.exe
      cs-class-1% ls                         <--- lists the directory
      first.cc  first.exe*
      cs-class-1% ./first.exe                <--- executes the program first.exe
      The sum is 11
      cs-class-1% rm first.exe               <--- removes first.exe
      cs-class-1% ls                         <--- lists the directory, which now contains only first.cc
      first.cc
      cs-class-1% g++ -o first.exe first.cc  <--- compiles first.cc to first.exe
      cs-class-1% ls                         <--- lists the directory, which now contains first.exe
      first.cc  first.exe*
      cs-class-1% ./first.exe                <--- runs the program first.exe
      The sum is 11
      cs-class-1% mv first.exe a.out         <--- moves first.exe to a.out
      cs-class-1% ls                         <--- lists the directory
      a.out*  first.cc
      cs-class-1% ./a.out                    <--- runs the program a.out
      The sum is 11
      cs-class-1% logout                     <--- terminates the session
      

Useful Linux and Unix Commands