UCL DEPARTMENT OF ELECTRONIC AND ELECTRICAL ENGINEERING

UCL logo

Solaris Linux Environment - Writing, Compiling and Running C++ and C Programs

Solaris Operating Environment

The instructions on this page are for the Solaris Operating Environment. Instructions for Sun Work Stations running the CentOS Linux Environment may be found on CentOS Linux Environment

1. Making Directories for your Programs
2. Writing your Program
3. Compiling your Program
4. Running your Program

5. A few useful UNIX commands


1. Making Directories for your Programs

You first need to create directories for your programs. You may make these either through UNIX commands using the terminal window or by using the file manager. You should familiarise yourself with both methods.

1.1 Using the terminal window
  • Open the terminal window.
    You may do this from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button, clicking on Hosts in the workspace menu that appears and then clicking on This Host.
  • Wait for the terminal window to open.
  • Check that you are in your home directory using the UNIX command pwd, if not use the UNIX command cd to get to your home directory.
  • It is sensible to make a directory for all your C-programming work, e.g. type the UNIX command

    mkdir CPP_Prog

    Generally, you may call the exercise directories by whatever names you wish within the UNIX rules. It is usual practice to name directories with a name that indicates their contents, e.g. CPP_Prog in the above case. A course lecturer may ask you to use specified names for some of your sub-directories if he or she is going to collect your course assignments directly from your home directory. In such cases it is important that you name the relevant directories exactly as requested paying attention to the case (upper or lower) of the letters of the specified names.
  • To enter the directory CPP_Prog, type the UNIX command

    cd CPP_Prog

  • It is sensible to make a directory for each exercise or assignment that you do, e.g.

    mkdir Exercise-1 (or mkdir Assignment-1 etc.)

  • The UNIX command, ls, will list all the directories and files in the directory in which you are currently working.
1.2 Using the file manager
  • Open the file manager.
    You may do this from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button and then clicking on Files in the work space menu that appears. Then click on File Manager.
  • Wait for the file manager window to open.
  • File manager should open displaying the files in your home directory. If it does not, double click on your home directory icon.
  • It is sensible to make a directory for all your C-programming work. Click on File on the menu bar. Then click on New Folder... in the drop-down menu. Type in the name of the folder, i.e. your new directory, in the dialogue box that now appears. It is helpful if you use a meaningful name e.g. CPP_Prog. Now click on the OK button. A course lecturer may ask you to use specified names for some of your sub-directories if he or she is going to collect your course assignments directly from your home directory. In such cases it is important that you name the relevant directories exactly as requested paying attention to the case (upper or lower) of the letters of the specified name.
  • To display the contents of the directory, CPP_Prog, double click on the CPP_Prog icon now displayed in your home directory. It will at this point be empty.
  • It is sensible to make a directory, within the directory CPP_Prog, for each exercise or assignment that you do, e.g.

    Exercise-1 (or Assignment-1 etc.)

2. Writing your Program

You must write your program using a text editor. You may use any text editor available. The two text editors, on the Departmental Sun Work Stations, that we think most appropriate are the standard text editor and the XEmacs text editor. The former is a very simple editor and is easy to use. The latter has several options that facilitate the writing and editing of C source code.

2.1 Standard Text Editor
This can be called from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button, and then clicking on Applications in the work space menu that appears. Then click on Text Editor.

2.1.1 Creating and saving a new file
Type your program on the page that appeared on calling the editor.
To save the file:
  • Click on File in the menu bar at the top of the Text Editor Window.
  • Click on the Save As ... command in the pull-down menu that appears.
  • A Dialogue Box now appears.
  • Double click on the directory, in which you wish to save the file, in the left hand folder box within the dialogue box. You may need to work through the directories on the path to the directory that you require.
  • Type the name of the file you wish to save in the box below the line, Enter file name:, within the dialogue box.
    If you are saving a c source code file remember to append the cpp (for a C++ source file) or c (for a C source file) extension, e.g. exercise_1.cpp NOT exercise_1.
  • Click on OK Button in dialogue box.
Once you have saved a file this way you may use the Save command rather than the Save As command. This will save the file in the text editor in the directory defined in the last Save As ... Command or, on opening a file (see below) in directory from which the file was opened, assuming you have not then used a Save As command.
Save your file frequently when creating it or editing it. That way you do not loose a significant amount of work if you accidentally do something foolish and unintentionally close the editor or crash the computer.

2.1.2 Opening an existing file
  • Click on File in the menu bar at the top of the Text Editor Window.
  • Click on the Open... command in the drop-down menu that appears.
  • A Dialogue Box now appears.
  • Double click on the directory you wish to open in the left hand folder box within the dialogue box. You WILL often need to change to the directory in which you save your files. Otherwise they will most likely end up in your home directory and not in the Exercise directory you made earlier.
  • Double click on file you wish to open.
You may now edit this file.

2.1.3 Identifying line numbers
You may continually monitor the line on which the cursor is placed. This option is valuable in editing a C program when the compiler has given the line numbers on which it errors have been found.

To continually monitor line numbers
  • Click on Options in the menu bar.
  • Click on Status Line in the drop down menu.
  • The row number for the row on which the cursor is placed and the total number of rows now show at the bottom of the text editor window.


3. Compiling your Program


3.1 Choice of compiler (C or C++)
You will usually find compilers for the commonly used languages, e.g. for the C language, the C++ language, for
Java, for Fortran and for Pascal, on most UNIX systems. There are two C/C++ compilers on the departmental system.
  • C program compilation
    The present standard compiler requires a compile command cc (see below). The lower case is obligatory for C program compilation. The second, i.e. gnu, compiler requires a compile command gcc (lower case). If you are using the gnu compiler, replace cc by gcc in all the commands listed below.
  • C++ program compilation
    Replace the lower case, cc, by upper case, CC, in the commands listed below to use the C++ compiler.

3.2 What the preprocessor, compiler and linker do
On compilation and linking the following occur:
  • The preprocessor/compiler copies your source code.
  • The code is first examined by the preprocessor which works on the copy leaving your source as written.
  • The preprocessor looks for include and define, i.e. #, statements and replaces the with appropriate code. The preprocessor passes the amended copied code to the compiler.
  • The compiler starts the compilation, checking for errors.
  • If no errors - compiler converts source code into object code.
  • If errors - compiler displays error message/s.
  • The linker combines object code with code required from libraries to produce an executable file.
3.3 Instructions on compiling your program (the UNIX compilation commands)
A C code source file called hello.c and the executable file called hello will be used as example files in describing the following instructions.

Open the terminal window if not already open.
Type one of the following UNIX cc commands (which ever is appropriate).
Remember spaces are significant!

  • 3.3.1 Compilation and linking in one step with a named executable file

    cc -o "executable file name" "C source file name"
    e.g. cc -o hello hello.c

    Compiles and links source hello.c and makes executable file hello, i.e. sets the output (-o) file to be called hello. It is conventional to call the executable file by the same name as the source file but without its .c extension.

    Check any error messages and go back to editor to make changes if required then save and compile file again.
    NB Always save the most recent version before compiling!

    Check your directory contents using either the UNIX command ls or the file manager.
    You should now have hello* (in the ls listing) or the hello icon with a lightning strike on it (in the file manager window) amongst your files, this is the EXECUtable FILE.

  • 3.3.2 Compilation and linking in one step without naming the executable file

    cc "C source file name"
    e.g. cc hello.c

    Compiles and links source hello.c and makes executable file with the default name a.out. Check your directory contents as above.
    You should now have a.out* (in the ls listing) or the a.out icon with a lightning strike on it (in the file manager window) amongst your files, this is the EXECUtable FILE.

    You may rename a file using the either the terminal window or the file manager. The UNIX command is

    mv "old name" "new name"
    e.g. mv a.out hello

    You can change a file name in file manager by clicking on the file icon, then clicking on Selection on the menu bar and then on Rename . . . in the drop-down menu. The cursur is now position within the file icon for you to change the name.

  • 3.3.3 Compilation without linking

    cc -c "C source file name"
    e.g. cc -c hello.c

    Compiles the source hello.c and makes an object file with the default name hello.o. Check your directory contents as above.
    You should now have a file called hello.o amongst your files, this is the object file.

  • 3.3.4 Linking an object file to make an executable file

    cc -o "executable file name" "object file name"
    e.g. cc -o hello hello.c

    Links the object file hello.o and makes an executable file hello.
3.4 Diagram showing compilation and linking




3.5 Using the mathematics library (math.h)


To include functions from the mathematics library you must:
  1. include the math.h header file, i.e. #include <math.h>
  2. instruct the linker to include the math library
    e.g. if program xxx.c includes the math.h header then the cc compile instruction becomes
    cc -o xxx xxx.c -lm
    The command -lm (minus lowercase el lowercase em) must come last as it is an instruction to the linker and not to the compiler.
    Specific linking, via -lm, of the math library is true of all UNIX C compilers, e.g. cc and gcc, but is not generally true of non-UNIX C compilers, e.g. Microsoft Visual C++.


3.6 A common mistake on compilation


The most common mistake on compilation, that we observe in students starting to program on a Sun work station, is to compile and run a program, note the error message, return to the editor and edit the source code, save it in a directory, e.g. Exercise_x_a, and then mistakenly compile and run an earlier uncorrected version that was saved in another directory, e.g. Exercise_x_b. Of course the error is not corrected in the version that has been run and the error message is again given.
Make sure that the directory in which you are saving your edited source code is the same one as that that you are working in when you compile and run your program.

4. Running your Program

To run your program, i.e. execute your executable file:
  • Open the terminal window if not already open.
  • Go to the directory in which the executable file is stored (cd command).
  • Type the name of the executable file, e.g. hello in the above examples, followed by a return.
  • The program will now execute.
  • If execution unsuccessful, edit the source file to correct errors and recompile. NB section 3.5 above.
5. A few useful UNIX commands

You will find the following commands either essential or useful in compiling C programs on the Departmental UNIX system.
pwd      Prints the working directory, i.e. tells you the directory in which you are working. See Section 1.1 above.
ls      Lists the files in the directory in which you are working. See Section 1.1 above.
cd xxx      Changes the directory in which you are working to the one named xxx. See Section 1.1 above.
mkdir xxx      Create a directory called xxx. See Section 1.1 above.
cc and gcc      C language compile command. See Section 3 above.
man unixcommand      Displays on the screen the listing, from the on-line UNIX manual, for whatever UNIX command you have typed in place of the word unixcommand.


In addition, the following commands may be useful when compiling and editing (they are specific to the C UNIX Shell, which is the shell, i.e. the part of the UNIX system that interprets user commands and passes them on to the kernel, in the departmental UNIX system).
!!      repeats the last command.
!n where n is a previous line number      repeats the command on line n.
!xx...      repeats the last command beginning with xx... .





Return to the top of this page
Return to Programming Courses main page


      This page is maintained by Michael Thomas Flanagan    -    last update: February 2009
      Department of Electronic and Electrical Engineering, University College London - Torrington Place - London - WC1E 7JE - Telephone: +44 (0)20 7679 7306 - Copyright © 1999-2009 UCL
      Disclaimer | Advanced Search |