在VC2005(Express版)中如何创建标准C++程序

来源:互联网 发布:如何戒淘宝成瘾 编辑:程序博客网 时间:2024/06/04 19:09

C++系列文章

(此系列文章或自写或翻译自其它技术文章,用于教学)

完整中文版:

Visual C++
创建标准 C++ 程序 (C++)

更新:2007 年 11 月

使用 Visual C++ 2008,您可以用 Visual Studio 开发环境创建标准 C++ 程序。在本过程中,您将创建一个新项目,向该项目添加一个新文件,修改该文件并添加 C++ 代码,然后使用 Visual Studio 编译并运行该程序。

您可以键入自己的 C++ 程序,或者使用示例程序之一。我们在此过程中使用的示例程序是一个控制台应用程序。此应用程序使用标准模板库 (STL) 中的 set 容器,该库是 ISO C++ 98 标准的一部分。

Visual C++ 符合下列标准:

  • ISO C 95

  • ISO C++ 98

  • Ecma C++/CLI 05

    在VC2005(Express版)中如何创建标准C++程序说明:

    如果要强制对程序进行 ANSI C++ 和 ANSI C 遵从性检查,应当使用 /Za 编译器选项(默认选项为 /Ze,允许对该标准的 Microsoft 扩展)。有关更多信息,请参见 /Za、/Ze(禁用语言扩展)

创建新项目并添加源文件

  1. 创建新项目:

    “文件”菜单上,指向“新建”,然后单击“项目”

  2. “Visual C++”项目类型中,单击“Win32”,然后单击“Win32 控制台应用程序”

  3. 输入项目名称。

    默认情况下,包含项目的解决方案与新项目同名,当然,您也可以键入其他名称。如果愿意,您可以为项目输入一个不同的位置。

    单击“确定”创建新项目。

  4. “Win32 应用程序向导”中,选择“空项目”并单击“完成”

  5. 如果“解决方案资源管理器”不可见,请单击“视图”菜单上的“解决方案资源管理器”

  6. 向该项目添加新的源文件:

    • 在“解决方案资源管理器”中,右击“源文件”文件夹,指向“添加”并单击“新建项”

    • 单击“代码”节点中的“C++ 文件(.cpp)”,输入一个文件名,然后单击“添加”

    .cpp 文件即显示在“解决方案资源管理器”中的“源文件”文件夹中,并且在键入代码的位置,出现一个选项卡式窗口。

  7. 在 Visual Studio 中,在新创建的选项卡中单击并键入使用标准 C++ 库的有效 C++ 程序,或者复制并粘贴示例程序之一。

    例如,您可以使用 set::find (STL Samples) 示例程序,该程序位于帮助中的“标准模板库示例”主题中。

    如果要为此过程使用示例程序,请注意 using namespace std; 指令。此指令将允许程序使用coutendl,而无需要求完全限定名(std::coutstd::endl)。

  8. “生成”菜单上,单击“生成解决方案”

    “输出”窗口显示有关编译过程的信息,如生成日志的位置,以及指示生成状态的消息。

  9. “调试”菜单上,单击“开始执行(不调试)”

    如果使用了示例程序,将显示一个命令窗口,其中显示是否在集合中找到了特定的整数。

中英文对照版

How to: Create a Standard C++ Program

如何创建标准C++程序(Liupq 译自MSDN)

With Visual C++ 2005, you can create Standard C++ programs using the Visual Studio Development Environment. In this procedure, you create a new project, add a new file to the project, edit the file to add C++ code, and then compile and run the program using Visual Studio.

在Visual C++ 2005,中,可以利用Visual Studio开发环境创建标准C++程序。其过程是:创建一个工程,然后在工程中添加一个新文件,在新文件中编写C++代码,再在Visual Studio中编译并运行程序。

You can type in your own C++ program or use one of the sample programs. The sample program referred to in this procedure is a console application that uses theset container in the Standard Template Library (STL), which is part of the ISO C++ 98 standard.

你可以输入你自己写的C++程序或是一个例程。这里的例程是指一个控制台应用程序,它使用STL中的SET容器(ISO C++ 98标准)。

Visual C++ conforms to these standards:

  • ISO C 95

  • ISO C++ 98

  • Ecma C++/CLI 05

Visual C++ 遵循下列标准:

  • ISO C 95

  • ISO C++ 98

  • Ecma C++/CLI 05

Note

You should use the /Za compiler option if you want to enforce ANSI C++ and ANSI C compliance checking of your program (the default option is/Ze which allows Microsoft extensions to the standard). See /Za, /Ze (Disable Language Extensions) for more information.

注意:

如果你想要与使你的程序ANSI C++ 与 ANSI C相容.可以使用/Za 选项。(缺省的选项是/Ze ,它允许Microsoft对标准进行扩展)。参阅/Za, /Ze (Disable Language Extensions)

To create a new project and add a source file

创建一个新的工程并添加一个源代码文件。

  1. Create a new project: (创建一个新工程;)

    On the File menu, point to New, then click Project….(在File菜单上,选择New->Project...)

  2. From the Visual C++ project types, click Win32, then clickWin32 Console Application.(从Visual C++工程类型中,点击win32,然后点击Win32 Console Application)

  3. Enter a project name. (输入工程的名字)

    By default, the solution that contains the project has the same name as the new project, though you can enter a different name. You can enter a different location for the project if you wish.

    Click OK to create the new project.

  4. In the Win32 Application Wizard, select Empty Project and clickFinish.

  5. If Solution Explorer is not visible, click Solution Explorer on theView menu.

  6. Add a new source file to the project:

    • Right-click on the Source Files folder in Solution Explorer and point toAdd and click New Item.

    • Click C++ File (.cpp) from the Code node, enter a file name, and then clickAdd.

    The .cpp file appears in the Source Files folder in Solution Explorer and a tabbed window appears where you type in the code.

  7. Click in the newly created tab in Visual Studio and type in a valid C++ program that uses the Standard C++ Library, or copy and paste one of the sample programs.

    For example, you can use the set::find (STL Samples) sample program in theStandard Template Library Samples topics in the help. See How to: Compile a Code Example from the Help Topics for information on copying a sample program to the Clipboard.

    If you use the sample program for this procedure, notice the using namespace std; directive. This allows the program to usecout and endl without requiring fully qualified names (std::cout andstd::endl).

  8. On the Build menu, click Build Solution.

    The Output window displays information about the compilation progress, such as the location of the build log and a message indicating that the build succeeded.

  9. On the Debug menu, click Start without Debugging.

    If you used the sample program, a command window is displayed that shows whether certain integers are found in the set.

原创粉丝点击