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

You can create Visual C++ programs that target the Windows Runtime and build them on the command line. Visual C++ supports Visual 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 Windows Phone 8.1, Windows Store, 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 contain 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 Windows Store apps. For more information, see Create a basic C++ Store app.

Prerequisites

You must 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 Visual C++ 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 Command Prompt window, see Setting the Path and Environment Variables for Command-Line Builds.

    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 are 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 have created a Visual C++ source file that uses the Windows Runtime Platform namespace (C++/CX) 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

Tasks

Visual C++ Guided Tour

Reference

Compiler Options

Other Resources

C++ Language Reference

Building C/C++ Programs