1.1.1 Compiling and Executing Our Program
Having written the program, we need to compile it. How you compile a program depends on your operating system and compiler. For details on how your particular compiler works, check the reference manual or ask a knowledgeable colleague.
Many PC-based compilers are run from an integrated development environment (IDE) that bundles the compiler with build and analysis tools. These environments can be a great asset in developing large programs but require a fair bit of time to learn how to use effectively. Learning how to use such environments is well beyond the scope of this book.
Most compilers, including those that come with an IDE, provide a commandline interface. Unless you already know the IDE, you may find it easier to start with the command-line interface. Doing so will let you concentrate on learning C++(www.cppentry.com) first. Moreover, once you understand the language, the IDE is likely to be easier to learn.
Program Source File Naming Convention
Whether you use a command-line interface or an IDE, most compilers expect program source code to be stored in one or more files. Program files are normally referred to as a source files. On most systems, the name of a source file ends with a suffix, which is a period followed by one or more characters. The suffix tells the system that the file is a C++(www.cppentry.com) program. Different compilers use different suffix conventions; the most common include .cc, .cxx, .cpp, .cp, and .C.
Running the Compiler from the Command Line
If we are using a command-line interface, we will typically compile a program in a console window (such as a shell window on a UNIX system or a Command Prompt window on Windows). Assuming that our main program is in a file named prog1.cc, we might compile it by using a command such as
- $ CC prog1.cc
where CC names the compiler and $ is the system prompt. The compiler generates an executable file. On a Windows system, that executable file is named prog1.exe. UNIX compilers tend to put their executables in files named a.out.
To run an executable onWindows, we supply the executable file name and can omit the .exe file extension:
- $ CC prog1.cc
On some systems you must specify the file’s location explicitly, even if the file is in the current directory or folder. In such cases, we would write
- $ CC prog1.cc
The “.” followed by a backslash indicates that the file is in the current directory.
To run an executable on UNIX, we use the full file name, including the file extension:
- $ CC prog1.cc
If we need to specify the file’s location, we’d use a “.” followed by a forward slash to indicate that our executable is in the current directory:
- $ CC prog1.cc
The value returned from main is accessed in a system-dependent manner. On both UNIX andWindows systems, after executing the program, you must issue an appropriate echo command.
On UNIX systems, we obtain the status by writing
- $ echo $
To see the status on aWindows system, we write
- $ echo %ERRORLEVEL%