Visual C++ 简介(针对 UNIX 用户)Introduction to Visual C++ for UNIX Users

本主题的内容适用于不熟悉 Visual C++ 且希望使用 Visual C++ 及 Visual Studio 集成开发环境 (IDE) 高效工作的 UNIX 用户。This topic provides information for UNIX users who are new to Visual C++ and want to become productive with Visual C++ and the Visual Studio Integrated Development Environment (IDE).

首先使用命令行Getting Started on the Command Line

可按使用 UNIX 命令行环境的相似方式来使用命令行中的 Visual C++。You can use Visual C++ from the command line in a similar way that you would use a UNIX command-line environment. 使用命令行 C 和 C++ 编译器 (CL.EXE)、链接器 (LINK.EXE) 以及包括 NMAKE.EXE(Microsoft 版 UNIX make 实用工具)在内的其他工具,从命令提示符进行编译。You compile from the command prompt by using the command-line C and C++ compiler (CL.EXE), linker (LINK.EXE), and other tools, including NMAKE.EXE, the Microsoft version of the UNIX make utility.

在 UNIX 中,命令安装在常用文件夹中,例如 /usr/bin。In UNIX, commands are installed in a common folder, such as /usr/bin. 在 Visual C++ 中,命令行工具安装在 VC\bin 子目录及其子目录的 Visual Studio 安装目录中。In Visual C++, the command-line tools are installed in your Visual Studio installation directory in the VC\bin subdirectory and its subdirectories. 不同于 UNIX,这些工具在纯命令提示符窗口中不可用。Unlike UNIX, these tools are not available in a plain command prompt window. 要使用命令行工具,请使用开发者命令提示符快捷方式,或者运行 vcvarsall.bat 等开发者命令文件。To use the command-line tools, use a developer command prompt shortcut, or run a developer command file such as vcvarsall.bat. 此操作设置路径和从命令行编译 Visual C++ 程序所必需的其他环境变量。This sets up the path and other environment variables that are necessary to compile Visual C++ programs from the command line. 有关详细信息,请参阅在命令行上生成 C/C++ 代码演练:在命令行上编译本机 C++ 程序For more information, see Build C/C++ code on the command line and Walkthrough: Compiling a Native C++ Program on the Command Line.

要打开开发者命令提示符快捷方式,请在桌面搜索控件中输入“开发者命令提示符”,然后选择适用于你的 Visual Studio 版本的“开发者命令提示符”结果。To open a developer command prompt shortcut, enter developer command prompt in the desktop search control and choose the Developer Command Prompt result for your version of Visual Studio. 要选择为特定主机和目标体系结构预配置的开发者命令提示符,请打开“开始”菜单(桌面一角的 Windows 图标),然后滚动到你的 Visual Studio 版本的文件夹,例如“Visual Studio 2017”。To choose a developer command prompt that is preconfigured for a particular host and target architecture, open the Start menu (the Windows icon in the corner of the desktop) and then scroll to the folder for your version of Visual Studio, such as Visual Studio 2017. 打开文件夹,选择首选主机和目标体系结构的命令提示符快捷方式。Open the folder and choose the command prompt shortcut for your preferred host and target architecture.

要利用更强大的功能(例如 Visual Studio 调试程序、IntelliSense 代码查找和语句完成、可视化设计器、项目管理等),需要使用 Visual Studio IDE。To take advantage of more powerful features, such as the Visual Studio debugger, IntelliSense code look-up and statement completion, visual designers, project management, and so on, you need to use the Visual Studio IDE.

调试代码Debugging Your Code

如果使用命令行并在开发工作站上运行应用程序,则在代码遇到内存访问冲突、未处理的异常或其他不可恢复的错误时,将显示运行 --- --- Visual StudioVisual Studio 调试器的对话框。If you use the command line and run your applications on your development workstation, you will see that a dialog box to run the --- --- Visual StudioVisual Studio debugger is displayed when your code encounters a memory access violation, unhandled exception, or other unrecoverable errors. 如果单击“确定”,将启动 Visual Studio 开发环境,并且在故障点之前调试器都将打开。If you click OK, then the Visual Studio development environment is started, and the debugger will open to the point of failure. 可按此方法调试应用程序,而且在这种情况下,只有在使用 /Z7、/Zi、/ZI(调试信息格式)交换机进行编译时,源代码才可用。It is possible to debug your applications this way, and, in this case, your source code would only be available if you compiled with the /Z7, /Zi, /ZI (Debug Information Format) switch. 有关详细信息,请参阅调试本机代码使用 Visual Studio IDE 进行 C++ 桌面开发For more information, see Debugging Native Code and Using the Visual Studio IDE for C++ Desktop Development.

使用开发环境Using the Development Environment

使用开发环境更容易在项目中编辑和生成源代码。It is easier to use the development environment to edit and build your source code in a project. 项目是指将编译到单个单元(如库或可执行文件)中的源文件和相关文件的集合。A project is a collection of source and related files that will be compiled into a single unit, such as a library or executable. 项目还包括文件生成方式的相关信息。A project also contains information on how the files are to be built. 项目的相关信息存储在扩展名为 .prj 的项目文件中。Information about projects is stored in a project file with the extension .prj.

单个解决方案中内含的多个项目中存储了包含多个库和可执行文件的应用程序,其中每个库或文件都可以一组不同的编译器选项,或甚至以不同语言生成。An application that consists of multiple libraries and executables, each potentially built with a different set of compiler options or even in a different language, are stored in multiple projects that are part of a single solution. 解决方案是容器的抽象概念,用来将多个项目组合在一起。A solution is an abstraction for a container to group multiple projects together. 解决方案的相关信息存储在扩展名为 .sln 的解决方案文件中。Information about solutions is stored in a solution file with the extension .sln. 有关详细信息,请参阅Visual Studio 中的解决方案和项目使用 Visual Studio IDE 进行 C++ 桌面开发For more information, see Solutions and Projects in Visual Studio and Using the Visual Studio IDE for C++ Desktop Development.

导入现有代码Importing Your Existing Code

可借助 Visual C++ 来生成设置为用(不用)生成文件进行编译的现有代码,并将它放入 --- --- Visual StudioVisual Studio 项目。You can use Visual C++ to build existing code that is set up to compile with or without a makefile and put it into a --- --- Visual StudioVisual Studio project. 有关详细信息,请参阅如何:通过现有代码创建 C++ 项目For more information, see How to: Create a C++ Project from Existing Code.

创建新项目Creating a New Project

可在开发环境中创建新项目。You can create new projects in the development environment. Visual C++ 提供了大量提供各种常见项目的标准代码的模板。Visual C++ provides numerous templates that provide standard code for various common projects. 可使用应用程序向导为各种应用程序类型生成具有代码大纲的项目。You can use application wizards to generate projects with code outlines for various application types.

可以使用“控制台应用程序 (Win32) 向导”开始创建空项目。You can start with an empty project by using the Console Application (Win32) Wizard. 选择“空项目”复选框。Select the Empty Project check box. 然后稍后可将新的和现有文件添加到项目。You can then add new and existing files to the project later.

当创建项目时,必须对其进行命名。When you create a project, you must name the project. 默认情况下,项目名称即为动态链接库 (DLL) 或从此项目生成的可执行文件的名称。By default, the project name equals the name of the dynamic-link library (DLL) or executable that is build from the project. 有关详细信息,请参阅创建解决方案和项目For more information, see Creating Solutions and Projects.

Microsoft 专用的修饰符Microsoft-Specific Modifiers

Visual C++ 包含一些对标准 C++ 编程语言的扩展,支持 Windows 操作系统编程。Visual C++ contains several extensions to the standard C++ programming language to support programming for Windows operating systems. 这些扩展用于指定存储类特性、函数调用约定和基于寻址以及其他用途。These extensions are used to specify storage class attributes, function calling conventions, and based addressing, among other things. 有关所有 Visual C++ 扩展的完整列表,请参阅 Microsoft 专用的修饰符For a complete list of all Visual C++ extensions, see Microsoft-Specific Modifiers.

可以使用 /Za 编译器选项禁用所有特定于 Microsoft 的 C++ 扩展。You can disable all Microsoft-specific extensions to C++ by using the /Za compiler option. 如果希望编写可在多个平台上运行的代码,建议使用此选项。This option is recommended if you want to write code to run on multiple platforms. 有关 /Za 编译器选项的详细信息,请参阅 /Za、/Ze(禁用语言扩展)For more information on the /Za compiler option, see /Za, /Ze (Disable Language Extensions). 有关 Visual C++ 一致性的详细信息,请参阅非标准行为For more information on Visual C++ conformance, see Nonstandard Behavior.

预编译标头Precompiled Headers

Microsoft C 和 C++ 编译器提供预编译任何 C 或 C++ 代码(包括内联代码)的选项。The Microsoft C and C++ compilers provide options for precompiling any C or C++ code, including inline code. 使用此性能功能,可以编译稳定的代码正文,在文件中存储已编译的代码状态,并在后续编译过程中将预编译代码和仍在开发的代码合并在一起。Using this performance feature, you can compile a stable body of code, store the compiled state of the code in a file, and, during subsequent compilations, combine the precompiled code with code that is still under development. 每个后续编译的速度都更快,因为无需重新编译稳定的代码。Each subsequent compilation is faster because the stable code does not need to be recompiled.

默认情况下,所有预编译的代码均在文件 stdafx.hstdafx.cpp 中进行指定。By default, all precompiled code is specified in the files stdafx.h and stdafx.cpp. “新建项目”向导将自动创建这些文件,除非取消选择“预编译标头”选项。The New Project wizard will automatically create these files for you unless you deselect the Precompiled header option. 有关预编译标头的详细信息,请参阅创建预编译标头文件For more information on precompiled headers, see Creating Precompiled Header Files.

有关详细信息,请参阅从 UNIX 到 Win32 的迁移For more information, see Porting from UNIX to Win32.

另请参阅See Also

生成 C/C++ 程序Building C/C++ Programs