Creating, Compiling, Linking and Executing C++ Programs
Let see how to invoke Turbo C++, how to name our programs, how to compile our programs, how to link our programs and how to execute our programs.
Invoking Turbo C++
To start Turbo C++, firstly we move to the directory in which we have installed our Turbo C++ software and then we will type TC at the DOS command prompt. If we have installed Turbo C++ in the root directory C:\> then we type TC as:
C:\>TC
As we type this and press Enter key we get the Turbo C++ IDE (Integrated Development Environment) screen that consists of a menu bar that contains menu options, such as File, Edit, Search, Run,
Figure1
Compile, and so on. It will mostly be blank, with the menu bar at the top and the status line on the bottom. The Turbo C++ includes all the facilities to develop and execute programs, such as editor, compiler, debugger, linker, and so on. The first character of each menu option is highlighted. You can select any menu option by pressing the ALT tab key and the highlighted character of the respective menu option. For example, if you want to select Edit menu option then you will press the TAB key and key containing the E character simultaneously as
ALT + E.
Naming Our Source Program
Now we are ready to work under Turbo C++ IDE . After invoking the Turbo C++ we select NEW option from the File menu. An edit window will appear with the filename NONAME00.CPP. Since it is the name specified by the Turbo C++ IDE, therefore you need to change this. For this we select the Save As option from the File menu. As we do this a dialog box will appear that asks for the name of our source program, for example CHAP0101.CPP, as shown in figure-****. After this the new name, that is CHAP0101.CPP, will appear at the top of the Edit window because we have changed the file name from the NONAME00.CPP to CHAP0101.CPP. Now the cursor is placed in the upper-left corner of the Edit window. Now we can type our source program, chap0101.cpp as shown in figure-2 as written in the earlier section, using arrow keys, tab key and Enter key to move the cursor.
Figure 2
Once we complete our typing we should save it to the disk by selecting Save from the File menu, or by pressing the F2. It is a good habit to this before compiling and executing our program.
Figure 3
Compiling The Source Program
The program that we type into the Edit window constitutes the source file, having .CPP extension. It is an ASCII file, you can not execute it directly. Therefore firstly it should be compiled into an object file. To compile the source file, we select Compile from Compile menu and will press Enter key. If there is no syntax error in the source program then the resulting window shows- Warning: 0, Errors: 0 and finally Success : Press any key. This compilation process creates an object file, which has an extension OBJ. However if there is any error then the compiler displays the appropriate error messages, called diagnostic messages, that tells the cause of the error and the compilation process terminates. Therefore go back to the source program, correct it and compile it again. You can also compile the source program by pressing the ALT and F9 keys simultaneously.
Figure-3 shows various stages of compilation of a C++ program.
Figure 4
Linking The Program
Once the source program has been converted into an object file, it is still not in the form that you can run. The reason behind this is that there may be some references to standard library functions or user-defined functions in other object files, which are compiled separately. Therefore these object files are to be linked together along with standard library functions that are contained in library files. To link our object file, select Link from the Compile menu. After this the .OBJ file will be compiled with the one or more library files. The result of this linking process produces an executable file with .EXE extension. Figure-4 depicts this linking process.
This chap0101.cpp program is a standalone executable program, which you can directly execute at command prompt as:
C:\TC> CHAP0101
This will execute the program and the results will get displayed on the screen.
Figure 5
Executing (Running) The Program
To execute (run) the .EXE file, select Run from RUN menu and press Enter key. Th result will be displayed on the screen and controls returns back to the editing screen. However you can also execute the .EXE file by pressing CTRL and F9 keys simultaneously. To see result select User Screen from WINDOW menu or by pressing ALT and F5 keys simultaneously.