Your Ad Here

COMPILING A C++ PROGRAM


The Visual C++ compiler is named cl. It is a program that is responsible for a two-step process
that takes your source code and transforms it into object code and then links the object code to
create executable code.
Object code is code in computer-readable form that is linked with libraries to create an executable
file. Executable code is the 1s and 0s that a computer needs to execute a program.


The C++ compiler automatically saves the object code in a file. This file has the same name
as the source code file, but it has an .obj extension rather than a .cpp extension.
The following steps show how to compile a source code file. These steps assume you have
already created and saved the HelloWorld.cpp source code file.
1. Set your PATH environment variable. Refer to “Read This Before You Begin” at the beginning
of this book for instructions on how to set the PATH environment variable.


2. Open a Command Prompt window. To do this in Windows XP, click the Start button, select
All Programs, select Accessories, and then select Command Prompt. In Vista, click the
Start button, select All Programs, select Accessories, and then select Command Prompt.
The cursor blinks to the right of the current file path. To compile your source code file, you
first have to change to the file path containing your source code file. To do this, type cd
driveletter:\path where driveletter is the drive containing your file, and path is
the path to the folder containing your file. For example, to gain access to a file stored in a
folder named Testing, which is in turn stored in a folder named My Program, which is
stored on the c: drive, you would type cd c:\My Program\Testing. After you type the
command, press Enter. The cursor now blinks next to the file path for the folder containing
your source code file.
3. Type the following command, which uses the C++ compiler , cl, to compile the program:
cl HelloWorld.cpp
If there are no syntax errors in your source code, a file named HelloWorld.obj and
a file named HelloWorld.exe are created. You do not see anything special happen.
However, the files you just created contain the object code (HelloWorld.obj) and executable
code (HelloWorld.exe) for the Hello World program. If there are syntax errors,
you will see error messages on the screen; in that case, you need to go back to Notepad to
fix the errors, save the source code file again, and recompile until there are no syntax
errors remaining. Syntax errors are messages from the compiler that tell you what your
errors are and where they are located in your source code file. For example, omitting a
semicolon at the end of the statement cout << "Hello World" << endl results
in a syntax error.


4. After the program is compiled, you can use the dir command to display a directory listing
to see the files named HelloWorld.obj and HelloWorld.exe. To execute the dir command,
you type dir at the command prompt. For example, if your source code file is
located at c:\My Program\Testing, the command prompt and dir command should look
like this: c:\My Program\Testing> dir. The HelloWorld.obj and HelloWorld.exe files
should be in the same directory as the source code file HelloWorld.cpp.



0 comments:

Post a Comment

Popular Posts

Recent posts