Creating an executable program consists of editing a file containing source code, using a compiler to translate the source code into machine code, and linking your main program with any functions, libraries or modules used.
Editing
Pico is probably the easiest editor to use initially. It is very similar to the editor included in the Pine mail reader program. To create a new file for editing you can simply type (at the Unix prompt symbol % shown here)
% pico
When you save the file you will be prompted to name it.
Alternatively, you can specify the name of the new file on the command line:
% pico myfile.cpp
To edit an already existing file, supply the name of the file to pico as above.
Compiling
To compile and link a source file using Compaq/Digital’s Compiler, type
% cxx hello.cpp
This will produce and executable file named "a.out". You can use the -o option to specify a name for your executable file:
% cxx -o hello hello.cpp
This results in an executable file named "hello" (hello.exe).
Note that you may use g++, the Gnu compiler, instead of cxx. Most Unix compilers, by default, do both the compiling and linking steps.
Executing after Compiling and Linking
To execute (or run) our program after compiling and linking, simply type the name of the file created during the compile and link. For example:
% a.out
% myprog
Other Combinations for Compiling and Linking
1. To compile a program (without linking) producing just an "object file" (.obj)
% cxx –c lookup.cpp
The object file, lookup.obj, can then be used later as part of a larger program system (see 3 and 4 below) or tested using a driver program as illustrated in 2. below.
2. To compile a program (myprog.cpp, perhaps a test driver) and link with a previously compiled object file (lookup.obj).
% cxx myprog.cpp lookup.obj
Note that any .h files associated with lookup.obj will have to be included in myprog.cpp as well as in lookup.cpp (used initially to compile and produce lookup.obj as shown in 1. above).
3. To link two previously compiled object files someprog.obj and lookup.obj and produce an executable file
% cxx somprog.cpp lookup.cpp
4. To link more than one previously compiled object file with a main program:
% cxx myprog.cpp mod1.obj mod2.obj mod3.obj
Diagnostics
An error message preceded with "cxx:" is a compiler error. This indicates that there is a problem in your source file. A message preceded with "ld:" is a linker error. Linker errors indicate problems with functions, libraries or modules.
You may find it helpful to keep a list of the more difficult errors you encounter, along with their eventual fixes.