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 SDK from version 1803 (10.0.17134.0) 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. 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 basiccx.cpp.

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

  4. 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.");
    }
    
  5. On the menu bar, choose File > Save.

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

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

  7. 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