2.C++ Primer(第5版)草译

 

最近公司效益不好,没什么项目,只能翻翻旧代码,看看资料,百无聊赖的时候,弄了一本《C++ Primer》(第5版)复习一下,同时顺便练习一下久违的英语翻译。由于我并不是英语专业出身的,而且也只是闲暇时草草地翻译,所以难免出现差错,欢迎提出批评指正。(*^__^*)

 

1.1.1. Compiling and Executing Our Program

1.1.1.编译并执行我们的程序

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.

很多基于PC端的编译器是在集成开发环境(IDE)中运行的,集成开发环境是把编译器与相关构建和分析工具捆绑而成的。对于开发大规模的程序而言,集成开发环境是一个很好的工具,但要学会高效地使用它也颇费时日。而学习如何使用集成开发环境也与本书的话题相去甚远。

 

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-line interface. 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而附带的)编译器都提供了一个命令行界面。除非你已经很熟悉你的IDE,否则,你可能会觉得使用命令行界面更容易上手一些。使用命令行界面会令你在学习初期把主要精力放在学习C++语言上。而且,一旦你了解了C++语言后, IDE可能也会变得更加容易学了。

 

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++ program. Different compilers use different suffix conventions; the most common include .cc, .cxx, .cpp, .cp, and .C.

不管你是用命令行界面还是IDE,大部分编译器都预期程序的源代码被保存在一个或者多个文件中。程序文件通常被称为源文件。在大部分系统中,源文件名都以一个后缀名(即一个句点号(period)后跟一个或多个字符)结尾。这个后缀名可以告知系统:这个文件是一个C++程序文件。不同的编译器使用不同的后缀名规范,其中最常用的包括:.cc,.cxx,.cpp, .cp和.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

如果是使用命令行界面的话,我们通常会在控制台窗口(例如,UNIX系统的shell窗口或是Windows的命令提示符窗口)中编译程序。假设我们的主程序(代码)是一个名为prog1.cc的文件,那么我们可能会用下面的命令来编译它:

    $ 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.

其中,CC为编译器名,$是系统提示符。执行了这个命令后,编译器会生成一个可执行文件。在Windows系统中,这个可执行文件被命名为prog1.exe,而UNIX系统的编译器则往往将可执行文件存放于名为a.out的文件中。

 

To run an executable on Windows, we supply the executable file name and can omit the .exe file extension:

要在Windows上运行一个可执行文件,我们只需提供可执行文件名,且可以省略.exe文件扩展名:

   $ prog1

 

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

在某些系统上,即使可执行文件存放在当前目录或文件夹下,你也要明确地指定该文件的存放位置。在这种情形下,我们会将该命令写为:

   $ .\prog1

 

The “.” followed by a backslash indicates that the file is in the current directory.

反斜杠后面的句点号“.”表明prog1文件存放于当前目录下。

 

To run an executable on UNIX, we use the full file name, including the file extension:

要在UNIX上运行一个可执行文件,我们会使用完整的文件名(包括文件扩展名):

   $ a.out

 

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:

如果我们要指定该文件的位置,我们可以使用一个句点号“.”后跟一个正斜杠以表明我们的可执行文件存放于当前目录下。

   $ ./a.out

 

The value returned from main is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command.

访问main函数返回值的方式是与系统相关的。不管是在UNIX还是在Windows系统上,执行完程序后,你都要发出一个适当的echo命令。

 

On UNIX systems, we obtain the status by writing

在UNIX系统上,我们可以编写下面的命令来获取(返回)状态:

   $ echo $?

 

To see the status on a Windows system, we write

要在Windows系统上查看返回状态,我们将该命令写为:

   $ echo %ERRORLEVEL%

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Running the GNU or Microsoft Compilers

运行GNU或Microsoft编译器

 

The command used to run the C++ compiler varies across compilers and operating systems. The most common compilers are the GNU compiler and the Microsoft Visual Studio compilers. By default, the command to run the GNU compiler is g++:

运行C++编译器的命令随着操作系统的不同而不同。GNU编译器和Microsoft Visual Studio编译器当属最常用的编译器。默认情况下,运行GNU编译器的命令是g++:

   $ g++ -o prog1 prog1.cc

 

Here $ is the system prompt. The -o prog1 is an argument to the compiler and names the file in which to put the executable file. This command generates an executable file named prog1 or prog1.exe, depending on the operating system. On UNIX, 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 UNIX systems and a.exe on Windows. (Note: Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support.)

在这里,$是系统提示符。-o prog1分别是传给编译器的参数和用于指定可执行文件的文件名。这个命令将生成一个名为prog1或prog1.exe的可执行文件,以哪一种方式命名则取决于所用的操作系统。可执行文件在UNIX系统上不带后缀名,在Windows系统上则带一个.exe的后缀名。如果-o prog1被省略掉的话,编译器会在UNIX系统上生成一个名为a.out的可执行文件,在Windows系统上则为a.exe。(请注意:你可能需要指定-std=c++这个参数以开启编译器对C++11的支持,是否需要这样做取决你所用的GNU编译器发布版本。)

 

The command to run the Microsoft Visual Studio 2010 compiler is cl:

运行Microsoft Visual Studio 2010编译器的命令是cl:

   C:\Users \ me\Programs> cl /EHsc prog1.cpp

 

Here C:\Users\me\Programs> is the system prompt and \Users\me\Programs is the name of the current directory (aka the current folder). The cl command invokes the compiler, and /EHsc is the compiler option that turns on standard exception handling. The Microsoft compiler automatically generates an executable with a name that corresponds to the first source file name. The executable has the suffix .exe and the same name as the source file name. In this case, the executable is named prog1.exe.

这里的 C:\Users\me\Programs> 是系统提示符,而 \Users\me\Programs 是当前目录(亦即当前文件夹)名。cl是调用编译器的命令,而/EHsc是开启编译器标准异常处理功能的选项。Microsoft编译器会自动生成一个可执行文件,这个可执行文件的文件名与第一个源文件的文件名相对应。可执行文件的文件名带有后缀.exe,且和源文件的文件名相同。在这个例子中,这个可执行文件被命名为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 -Wall with the GNU compiler, and to use /W4 with the Microsoft compilers.

对于一些有问题的程序构建过程,编译器通常包含了相应的选项以生成警告。一般而言,使用这些选项是一个不错的主意。对于GNU编译器,我们倾向于使用选项-Wall,Microsoft的编译器则用/W4。

 

For further information consult your compiler’s user’s guide.

你也可以查询你的编译器用户指南以获取更详细的信息。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Exercises Section 1.1.1

练习

 

Exercise 1.1: Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2.

习题 1.1:查阅你的编译器帮助文档并确定它使用哪一种文件命名规范。然后,编译并运行第2页的main程序。

 

Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.

习题 1.2:修改main程序使它返回-1(return -1)。 一个-1的返回值通常被当作程序运行失败的指示器。 重新编译并再次运行你的程序,看看你的系统是如何处理从main程序返回的运行失败指示器的。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

posted on 2013-01-30 23:14  caesarchow  阅读(736)  评论(0编辑  收藏  举报

导航