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

Note

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 is available in the Windows 10 SDK from version 1803 onward. C++/WinRT is implemented entirely in header files, and is designed to provide you with first-class access to the modern Windows API.

The Microsoft C++ compiler (MSVC) supports C++ component extensions (C++/CX), which has additional types and operators to target the Windows Runtime programming model. You can use C++/CX to build apps for Universal Windows Platform (UWP), and Windows desktop. For more information, see A Tour of C++/CX and Component Extensions for Runtime Platforms.

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

Note

You can also use the Visual Studio IDE to compile C++/CX programs. 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. For more information, see Create a UWP app in C++.

Prerequisites

You understand the fundamentals of the C++ language.

Compiling a C++/CX Program

To enable compilation for C++/CX, you must use the /ZW compiler option. The MSVC compiler generates an .exe file that targets the Windows Runtime, and links to the required libraries.

To compile a C++/CX application on the command line

  1. Open a Developer Command Prompt window. (On the Start window, open Apps. 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. 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.

    You've created a C++ source file that uses the Windows Runtime Platform namespace namespace.

  5. At the command prompt, enter cl /EHsc /ZW basiccx.cpp /link /SUBSYSTEM:CONSOLE. 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. (The /EHsc compiler option specifies the C++ exception-handling model, and the /link flag specifies a console application.)

  6. 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 Compiler Options