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

可以创建面向公共语言运行时 (CLR) 并使用 .NET Framework 的 Visual C++ 程序,然后在命令行上生成这些程序。You can create Visual C++ programs that target the Common Language Runtime (CLR) and use the .NET Framework, and build them on the command line. Visual C++ 支持 C++/CLI 编程语言,它具有要面向 .NET 编程模型的其他类型和运算符。Visual C++ supports the C++/CLI programming language, which has additional types and operators to target the .NET programming model. 有关 C++/CLI 语言的常规信息,请参阅使用 C++/CLI (Visual C++) 进行 .NET 编程For general information about the C++/CLI language, see .NET Programming with C++/CLI (Visual C++).

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

先决条件Prerequisites

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

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

以下步骤显示如何编译使用 .NET Framework 类的 C++/CLI 控制台应用程序。The following steps show how to compile a C++/CLI console application that uses .NET Framework classes.

若要启用 C++/CLI 的编译,你必须使用 /clr 编译器选项。To enable compilation for C++/CLI, you must use the /clr compiler option. MSVC 编译器将生成包含 MSIL 代码(或 混合 MSIL 和本机代码)的 .exe 文件,并链接到所需的 .NET Framework 库。The MSVC compiler generates an .exe file that contains MSIL code—or mixed MSIL and native code—and links to the required .NET Framework libraries.

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

  1. 打开“开发人员命令提示” 窗口。Open a Developer Command Prompt window. 有关具体说明,请参阅打开“开发人员命令提示”窗口For specific instructions, see To open a developer command prompt window.

    可能需要管理员凭据才能成功编译此代码,取决于计算机的操作系统和配置。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, right-click to open the shortcut menu for the command prompt and then choose More > Run as administrator.

  2. 在命令提示符处,输入 notepad basicclr.cppAt the command prompt, enter notepad basicclr.cpp.

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

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

    int main()
    {
        System::Console::WriteLine("This is a C++/CLI program.");
    }
    
  4. 在菜单栏上,选择“文件” > “保存” 。On the menu bar, choose File > Save.

    你已经在 System 命名空间中创建了一个使用 .NET Framework 类 (Console) 的 Visual C++ 源文件。You've created a Visual C++ source file that uses a .NET Framework class (Console) in the System namespace.

  5. 在命令提示符处,输入 cl /clr basicclr.cppAt the command prompt, enter cl /clr basicclr.cpp. cl.exe 编译器将源代码编译到包含 MSIL 的 .obj 文件中,然后运行链接器来生成名为 basicclr.exe 的可执行程序。The cl.exe compiler compiles the source code into an .obj file that contains MSIL, and then runs the linker to generate an executable program named basicclr.exe.

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

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

    This is a C++/CLI program.
    

请参阅See also

C++ 语言参考C++ Language Reference
项目和生成系统Projects and build systems
MSVC 编译器选项MSVC Compiler Options