Share via


Walkthrough: Compiling a Native C++ Program on the Command Line (C++)

Visual C++ includes a C++ compiler that you can use to create everything from basic Visual C++ programs to Windows Forms applications and components.

By following this walkthrough, you can create basic Visual C++ programs by using a text editor, and then compile them on the command line.

You can also compile Visual C++ programs that you created by using the Visual Studio integrated development environment (IDE). For more information, see Walkthrough: Compiling a C++ Program that Targets the CLR in Visual Studio (C++).

You can use your own Visual C++ programs instead of typing the programs shown in the following steps. You can also use any of the Visual C++ code sample programs in other help topics.

Prerequisites

To complete this walkthrough, you must understand the fundamentals of the C++ language.

To create a Visual C++ source file and compile it on the command line

  1. Open the Visual Studio 2010 Command Prompt window by clicking Start, pointing to All Programs, Microsoft Visual Studio 2010, Visual Studio Tools, and then clicking Visual Studio 2010 Command Prompt.

    Administrator credentials may be required to successfully compile the code in this walkthrough, depending on the operating system and configuration of the computer. To run the Visual Studio 2010 Command Prompt window as an administrator, right-click Visual Studio 2010 Command Prompt and then click Run as administrator.

  2. At the command prompt, type notepad basic.cpp and press Enter.

    Click Yes when you are prompted to create a file.

  3. In Notepad, type the following lines.

    #include <iostream>
    
    int main()
    {
        std::cout << "This is a native C++ program." << std::endl;
        return 0;
    }
    
  4. On the File menu, click Save.

    You have created a Visual C++ source file.

  5. Close Notepad.

  6. At the command prompt, type cl /EHsc basic.cpp and press Enter. The /EHsc command-line option instructs the compiler to enable C++ exception handling. For more information, see /EH (Exception Handling Model).

    The cl.exe compiler generates an executable program named basic.exe.

    You can see the executable program name in the lines of output information that the compiler displays.

  7. To see a list of files in the directory that have the name basic together with any file name extension, type dir basic.* and press Enter.

    The .obj file is an intermediate format file that you can safely ignore.

  8. To run the basic.exe program, type basic and press Enter.

    The program displays this text and exits:

    This is a native C++ program.

  9. To close the Visual Studio 2010 Command Prompt window, type exit and press Enter.

Compiling a Visual C++ Program That Uses .NET Classes

The following steps show how to compile a Visual C++ program that uses .NET Framework classes.

You must use the /clr (Common Language Runtime Compilation) compiler option because this program uses .NET classes and must include the required .NET libraries. The Visual C++ compiler generates an .exe file that contains MSIL code instead of machine executable instructions.

Follow the steps in this procedure to compile any sample Visual C++ program in the help topics.

To compile a Visual C++ .NET console application on the command line

  1. Open the Visual Studio 2010 Command Prompt window by clicking Start, pointing to All Programs, Microsoft Visual Studio 2010, Visual Studio Tools, and then clicking Visual Studio 2010 Command Prompt.

    Administrator credentials may be required to successfully compile the code in this walkthrough, depending on the operating system and configuration of the computer. To run the Visual Studio 2010 Command Prompt window as an administrator, right-click Visual Studio 2010 Command Prompt and then click Run as administrator.

  2. At the command prompt, type notepad basicclr.cpp and press Enter.

    Click Yes when you are prompted to create a file.

  3. In Notepad, type the following lines.

    int main()
    {
        System::Console::WriteLine("This is a Visual C++ program.");
    }
    
  4. On the File menu, click Save.

    You have created a Visual C++ source file that uses a .NET class (Console) and is located in the System namespace.

  5. Close Notepad.

  6. At the command prompt, type cl /clr basicclr.cpp and press Enter. The cl.exe compiler generates an executable program named basicclr.exe.

  7. To see a list of files in the directory that have the name basicclr together with any file name extension, type dir basicclr.* and press Enter.

    The .obj file is an intermediate format file that you can safely ignore.

    The .manifest file is an XML file that contains information about the assembly. (An assembly is the .NET unit of deployment, for example, an .exe program or .dll component or library.)

  8. To run the basicclr.exe program, type basicclr and press Enter.

    The program displays this text and exits:

    This is a Visual C++ program.

  9. To close the Visual Studio 2010 Command Prompt window, type exit and press Enter.

Next Steps

Previous: Walkthrough: Creating a Standard C++ Program (C++). Next: Walkthrough: Compiling a C++ Program that Targets the CLR in Visual Studio (C++).

See Also

Tasks

Visual C++ Guided Tour

Reference

Compiler Options

Other Resources

C++ Language Reference

Building a C/C++ Program