Bewerken

Delen via


Walkthrough: Compiling a C++/CLI Program on the Command Line

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++ supports the C++/CLI programming language, which has additional types and operators to target the .NET programming model. For general information about the C++/CLI language, see .NET Programming with C++/CLI (Visual C++).

In this walkthrough, you use a text editor to create a basic C++/CLI program, and then compile it on the command line. (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. This technique is useful for building and testing small modules that have no UI elements.)

Prerequisites

You understand the fundamentals of the C++ language.

Compiling a C++/CLI Program

The following steps show how to compile a C++/CLI console application that uses .NET Framework classes.

To enable compilation for C++/CLI, you must use the /clr compiler option. 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.

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. Change the current working directory in the command prompt window to a directory you can write to, such as your Documents directory.

  3. At the command prompt, enter notepad basicclr.cpp.

    Choose Yes when you're prompted to create a file.

  4. In Notepad, enter these lines:

    int main()
    {
        System::Console::WriteLine("This is a C++/CLI program.");
    }
    
  5. On the menu bar, choose File > Save.

    You've created a Visual C++ source file that uses a .NET Framework class (Console) in the System namespace.

  6. At the command prompt, enter cl /clr basicclr.cpp. 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.

  7. To 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++ Language Reference
Projects and build systems
MSVC Compiler Options