C++程序的编译和运行



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 yourparticular compiler works, check the reference manual or ask a knowledgeablecolleague.
程序写完了,我们需要对它进行编译。如何编译一个程序取决于操作系统和编译器。
Many PC-based compilers are run from an integrated development environment (IDE)that bundles the compiler with build and analysis tools. These environments canbe a great asset in developing large programs but require a fair bit of time tolearn how to use effectively. Learning how to use such environments is wellbeyond the scope of this book.
Most compilers, including those that come with an IDE, provide a command-line interface.Unless you already know the IDE, you may find it easier to start with the command-lineinterface. Doing so will let you concentrate on learning C++ first. Moreover,once you understand the language, the IDE is likely to be easier to learn.
很多编译器,包括IDE中的,提供一个命令行接口
Program SourceFile Naming Convention程序源文件命名约定
Whether you use acommand-line interface or an IDE, most compilers expect program source code tobe stored in one or more files. Program files are normally referred to as a source files. On most systems, the name ofa source file ends with a suffix, which is a period followed by one or morecharacters. The suffix tells the system that the file is a C++ program.Different compilers use different suffix conventions; the most common include.cc, .cxx, .cpp, .cp, and .C.不同的编译器使用不同的后缀约定;最常见的包括.cc.cxx.cpp.cp.C
Running theCompiler from the Command Line从命令行运行编译器
If we are using acommand-line interface, we will typically compile a program in a console window(such as a shell windowshell窗口) on a UNIX system or a CommandPrompt 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 generatesan executable file. On a Windows system, that executable file is namedprog1.exe. UNIX compilers tend to put their executables in files named a.out.
To run an executable on Windows, we supply the executable file name and canomit the .exe file extension:
$ prog1
On some systems you must specify the file’s location explicitly, even if thefile is in the current directory or folder. In such cases, we would write
$ .\prog1
The
. followed by a backslash indicates thatthe file is in the current directory.
To run an executable on UNIX, we use the full file name, including the file extension:
$ a.out
If we need to specify the file
s location, wed use a . followedby a forward slash to indicate that our executable is in the current directory:
$ ./a.out
The value returned from main is accessed in a system-dependent manner. On both UNIXand Windows systems, after executing the program, you must issue an appropriateecho command.
On UNIX systems, we obtain the status by writing
$ echo $?
To see the status on a Windows system, we write
$ echo %ERRORLEVEL%
Running the GNU orMicrosoft Compilers
运行GNU或微软编译器
The command used to run the C++ compiler varies across compilers and operatingsystems. The most commoncompilers are the GNU compiler and the Microsoft Visual Studio compilers.By default, the command to run the GNU compiler is g++:
Click here to view code image
$g++ -o prog1 prog1.cc
Here $ is the system prompt. The -o prog1 is an argument to the compiler andnames the file in which to put the executable file. This command generates anexecutable file named prog1 or prog1.exe, depending on the operating system. OnUNIX, executable files have no suffix; on Windows, the suffix is .exe. If the-o prog1 is omitted, the compiler generates an executable named a.out on UNIXsystems and a.exe on Windows. (Note: Depending on the release of the GNUcompiler you are using, you may need to specify -std=c++0x to turn on C++ 11support.)
The command to run the Microsoft Visual Studio 2010 compiler is cl:
C:\Users\me\Programs> cl /EHsc prog1.cpp
Here C:\Users\me\Programs> is the system prompt and \Users\me\Programs isthe name of the current directory (aka the current folder). The cl commandinvokes the compiler, and /EHsc is the compiler option that turns on standardexception handling. The Microsoft compiler automatically generates anexecutable with a name that corresponds to the first source file name. Theexecutable has the suffix .exe and the same name as the source file name. Inthis case, the executable is named prog1.exe.
Compilers usually include options to generate warnings about problematic constructs.It is usually a good idea to use these options. Our preference is to use -Wallwith the GNU compiler, and to use /W4 with the Microsoft compilers.
For further information consult your compiler
s users guide.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值