演练:在命令行上编译 C++/CX 程序Walkthrough: Compiling a C++/CX Program on the Command Line

备注

对于新 UWP 应用和组件,建议使用 C++/WinRT,这是 Windows 运行时 API 的 标准 C++17 语言投影。For new UWP apps and components, we recommend that you use C++/WinRT, a standard C++17 language projection for Windows Runtime APIs. C++/WinRT 从版本 1803 起在 Windows 10 SDK 中提供。C++/WinRT is available in the Windows 10 SDK from version 1803 onward. C++/WinRT 完全在头文件中实现,旨在提供对新式 Windows API 的优先访问权限。C++/WinRT is implemented entirely in header files, and is designed to provide you with first-class access to the modern Windows API.

Microsoft C++ 编译器 (MSVC) 支持 C++ 组件扩展 (C++/CX),其中具有面向 Windows 运行时编程模型的其他类型和运算符。The Microsoft C++ compiler (MSVC) supports C++ component extensions (C++/CX), which has additional types and operators to target the Windows Runtime programming model. 可使用 C++/CX 为通用 Windows 平台 (UWP) 和 Windows 桌面生成应用。You can use C++/CX to build apps for Universal Windows Platform (UWP), and Windows desktop. 有关详细信息,请参阅 C++/CX 教程运行时平台的组件扩展For more information, see A Tour of C++/CX and Component Extensions for Runtime Platforms.

在此演练中,你将使用文本编辑器创建一个基本的 C++/CX 程序,然后在命令行上对其进行编译。In this walkthrough, you use a text editor to create a basic C++/CX program, and then compile it on the command line. (可使用你自己的 C++/CX 程序,而非键入显示的程序,或者也可使用来自另一篇帮助文章中的 C++/CX 代码示例。(You can use your own C++/CX program instead of typing the one that's shown, or you can use a C++/CX code sample from another help article. 这种技术有助于生成和测试不包含 UI 元素的小模块。)This technique is useful for building and testing small modules that have no UI elements.)

备注

还可使用 Visual Studio IDE 来编译 C++/CX 程序。You can also use the Visual Studio IDE to compile C++/CX programs. 由于 IDE 包含命令行上不可用的设计、调试、仿真和部署支持,因此建议使用 IDE 来生成通用 Windows 平台 (UWP) 应用。Because the IDE includes design, debugging, emulation, and deployment support that isn't available on the command line, we recommend that you use the IDE to build Universal Windows Platform (UWP) apps. 有关详细信息,请参阅在 C++ 中创建 UWP 应用For more information, see Create a UWP app in C++.

先决条件Prerequisites

了解 C++ 语言的基础知识。You understand the fundamentals of the C++ language.

编译 C++/CX 程序Compiling a C++/CX Program

若要启用 C++/CX 的编译,你必须使用 /ZW 编译器选项。To enable compilation for C++/CX, you must use the /ZW compiler option. MSVC 编译器将生成一个面向 Windows 运行时的 .exe 文件,并链接到所需的库。The MSVC compiler generates an .exe file that targets the Windows Runtime, and links to the required libraries.

在命令行上编译 C++/CX 应用程序To compile a C++/CX application on the command line

  1. 打开“开发人员命令提示” 窗口。Open a Developer Command Prompt window. (在“开始” 窗口上,打开“应用” 。(On the Start window, open Apps. 在你的 Visual Studio 版本下打开“Visual Studio Tools” 文件夹,然后选择“开发人员命令提示” 快捷方式。)有关如何打开“开发人员命令提示”窗口的详细信息,请参阅通过命令行使用 MSVC 工具集Open the Visual Studio Tools folder under your version of Visual Studio, and then choose the Developer Command Prompt shortcut.) For more information about how to open a Developer Command Prompt window, see Use the MSVC toolset from the command line.

    可能需要管理员凭据才能成功编译此代码,取决于计算机的操作系统和配置。Administrator credentials may be required to successfully compile the code, depending on the computer's operating system and configuration. 若要以管理员身份运行命令提示窗口,请打开“开发人员命令提示” 的快捷菜单,然后选择“以管理员身份运行” 。To run the Command Prompt window as an administrator, open the shortcut menu for Developer Command Prompt and then choose Run as administrator.

  2. 在命令提示符下,输入 notepad basiccx.cpp 。At the command prompt, enter notepad basiccx.cpp.

    在系统提示是否创建文件时,选择“是” 。Choose Yes when you're prompted to create a file.

  3. 在记事本中,输入以下行:In Notepad, enter these lines:

    using namespace Platform;
    
    int main(Platform::Array<Platform::String^>^ args)
    {
        Platform::Details::Console::WriteLine("This is a C++/CX program.");
    }
    
  4. 在菜单栏上,选择“文件” > “保存” 。On the menu bar, choose File > Save.

    你已创建使用 Windows 运行时 Platform 命名空间命名空间的 C++ 源文件。You've created a C++ source file that uses the Windows Runtime Platform namespace namespace.

  5. 在命令提示符下,输入 cl /EHsc /ZW basiccx.cpp /link /SUBSYSTEM:CONSOLE 。At the command prompt, enter cl /EHsc /ZW basiccx.cpp /link /SUBSYSTEM:CONSOLE. cl.exe 编译器将源代码编译到 .obj 文件中,然后运行链接器以生成名为 basiccx.exe 的可执行程序。The cl.exe compiler compiles the source code into an .obj file, and then runs the linker to generate an executable program named basiccx.exe. /EHsc 编译器选项指定 C++ 异常处理模型,而 /link 标志指定控制台应用程序。)(The /EHsc compiler option specifies the C++ exception-handling model, and the /link flag specifies a console application.)

  6. 若要运行 basiccx.exe 程序,请在命令提示符下,输入 basiccx 。To run the basiccx.exe program, at the command prompt, enter basiccx.

    该程序显示以下文本并退出:The program displays this text and exits:

    This is a C++/CX program.
    

请参阅See also

项目和生成系统Projects and build systems
MSVC 编译器选项MSVC Compiler Options